public class Customer : BaseClass<Customer>
{
public string Name { get; set; }
public DateTime? DateOfBirth { get; set; }
public string TelephoneNumber { get; set; }
public string InsuranceProvider { get; set; }
public int? PolicyNumber { get; set; }
public byte [] Photo { get; set; }
}
This message shows up:

Or, this:

How to modify this code to be able to persist data?
CustomerDAO.cs
class CustomerDAO
{
.....
public int Save(ITransactionManager tm, Customer item)
{
int count = -1;
try
{
ISqlQueryExecutor<Customer> queryExecutor = new SqlQueryExecutor<Customer>(tm);
count =
queryExecutor.
ExecuteNonQuery(@"INSERT INTO Customer(
ID
,Name
,TelephoneNumber
,DateOfBirth,
InsuranceProvider,
PolicyNumber)
VALUES(
@ID
,@Name
,@TelephoneNumber
,@DateOfBirth,
@InsuranceProvider,
@PolicyNumber)",
item.ID,
item.Name,
item.TelephoneNumber,
item.DateOfBirth,
item.InsuranceProvider,
item.PolicyNumber,item.Photo
);
//new DbParameter(item.ID, DbType.Int32),
//new DbParameter(item.Name, DbType.String),
//new DbParameter(item.TelephoneNumber, DbType.String),
//new DbParameter(item.DateOfBirth, DbType.DateTime),
//new DbParameter(item.InsuranceProvider, DbType.String),
//new DbParameter(item.PolicyNumber, DbType.Int32)
//new DbParameter(item.Photo, DbType.Binary)
//);
string str = string.Empty;
}
catch (Exception ex)
{
throw ex;
}
return count;
}
.... ....
}
CustomerBLL.cs
class CustomerBLL
{
... ... ...
public int Save(Customer item)
{
int newId = 0;
ITransactionManager tm = ApplicationContext.Get(DBNameConst.ActiveConnStringName);
try
{
tm.BeginTransaction();
item.ID = newId = PivotTable.GetNextID(tm, "Customer").Value;
customerDao.Save(tm, item);
PivotTable.UpdateNextIdField(tm, "Customer", newId);
tm.CommitTransaction();
}
catch (Exception ex)
{
tm.RollbackTransaction();
throw ex;
}
return newId;
}
... ... ...
}
ASqlQueryExecutor.cs
public abstract class ASqlQueryExecutor<T> : ISqlQueryExecutor<T>
{
public virtual int ExecuteNonQuery(string queryString, params object[] parameters)
{
int count = -1;
try
{
Command = ParameterAttacher.AttachSaveParameters(TransactionManager, queryString, parameters);
Command.CommandText = queryString;
count = Command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
return count;
}
ParameterAttacher.cs
class ParameterAttacher
{
public static IDbCommand AttachSaveParameters(ITransactionManager tm, string queryString, params object [] argumentsList)
{
IDbCommand command = new DbObjectInstantiator(tm.ProviderName).CreateCommand();
command.Connection = tm.Connection;
command.Transaction = tm.Transaction;
IList<string> parameterNamesList = new List<string>(ParameterParser.Parse(queryString));
if (parameterNamesList.Count > 0 && argumentsList.Length == argumentsList.Length)
{
int i = 0;
foreach (string paramName in parameterNamesList)
{
Attach(command, paramName, argumentsList[i]);
++i;
}
}
return command;
}
public static void Attach(IDbCommand command, string paramName, object dbParam)
{
IDbDataParameter param = command.CreateParameter();
param.ParameterName = paramName;
param.Value = (dbParam==null) ? ((object)DBNull.Value) : dbParam;
//param.DbType = dbParam.DbType;
command.Parameters.Add(param);
}
}

This could occur because you’re trying to store too much data into a column. For example, if you have an nvarchar(5) column in a database and you try to store “this is a string” in there, you might get that error because 5 characters can’t hold all of “this is a string”.
You can avoid this problem by limiting the fields in your UI to the same length as those in the database. Or, you can perform a check in the validation methods.
This seems fairly obvious: you’re trying to store a character value in a binary column. You haven’t provided your database schema, so I can’t tell for sure where this could be; but, if you have a column or sproc parameter in the database set as
binarybut your C# details it asDbType.Stringyou might get this error.UPDATE:
You never set your
DbTypeinParameterAttacher.Attach. This means theParameterwill default toDbType.AnsiStringfor the parameter type. If you pass it abyte[]it may convert that to an ansi string, but when the parameter is given to ADO, it will see DbType.AnsiString and compare that tovarbinary(50)(ormoney, ordatetime, orint, etc.) and throw an exception detailing that it doesn’t know how to convert tobinary(e.g. an implicit conversion).Also, do yourself a favour and get rid of:
That will just force you to loose the real location of the exception and cause you to waste time trying to figure out where the real problem is.
When you must catch (e.g. when you want to rollback the transaction, just
throw, don’tthrow ex. Justthrowwon’t lose the stack information and you can track down the location of the exception. For example: