I'm trying to insert into a table in a C# program. I have this insert command:
var insertSql = @"INSERT INTO dbo.[Case]
VALUES (@Id, @IsDeleted, @CaseNumber, @ContactId, @AccountId, @ParentId, @SuppliedName, @SuppliedEmail, @SuppliedPhone, @SuppliedCompany, @Type, @RecordTypeId, @Status, @Reason, @Origin...
And then I've got many lines adding in the parameters like so:
var command = new SqlCommand(insertSql, easySoftConn);
if (case2.Id != null)
command.Parameters.AddWithValue("@Id", case2.Id);
else
command.Parameters.AddWithValue("@Id", DBNull.Value);
if (case2.IsDeleted != null)
{
if (case2.IsDeleted == "true")
command.Parameters.AddWithValue("@IsDeleted", 1);
else
command.Parameters.AddWithValue("@IsDeleted", 0);
}
else
command.Parameters.AddWithValue("@IsDeleted", DBNull.Value);
if (case2.CaseNumber != null)
command.Parameters.AddWithValue("@CaseNumber", case2.CaseNumber);
else
command.Parameters.AddWithValue("@CaseNumber", DBNull.Value);
if (case2.ContactId != null)
command.Parameters.AddWithValue("@ContactId", case2.ContactId);
else
command.Parameters.AddWithValue("@ContactId", DBNull.Value);
...
When I finally execute the insert:
try
{
command.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException e)
{
CLog.Write(e.Message.ToString(), CLog.ErrLvl.Error);...
}
I get the error:
String or binary data would be truncated
My issue is, the error doesn't tell me which column would be truncated. I've got 80 columns I'm inserting into, and I'd rather not go through them one-by-one. Is there a way to get the error handling to tell me exactly which field is throwing the error?
EDIT: I have a full stack trace in my log file but it still doesn't tell me which column, I just shortened it to the actual error here.
Looks like the value of one or more of your parameters has more length than the table cell can contain. You should to look at table column definitions.