Reputation: 63
I'm getting the following error message:
string or binary data would be truncated
I've tried increasing the column size but no luck, I've doubled checked by code but cant seem to find any issues. Its during an insert:
SqlCommand insert = new SqlCommand(@"INSERT
into orderDetails
(orderID, Name, Phone, Mobile, Email, DelName, DelRoad, DelTown, DelCity, DelCounty, DelPostCode, BilName, BilRoad, BilTown, BilCity, BilCounty, BilPostCode)
values
(@orderID , @Name , @Phone , @Mobile , @Email , @DelName , @DelRoad , @DelTown , @DelCity , @DelCounty , @DelPostCode , @BilName , @BilRoad , @BilTown , @BilCity , @BilCounty , @BilPostCode)", connection);
insert.Parameters.AddWithValue("@orderID", ID);
insert.Parameters.AddWithValue("@Name", name);
insert.Parameters.AddWithValue("@Phone", customer.Phone);
insert.Parameters.AddWithValue("@Mobile", customer.Mobile);
insert.Parameters.AddWithValue("@Email", customer.Email);
insert.Parameters.AddWithValue("@DelName", customer.DelName);
insert.Parameters.AddWithValue("@DelRoad", customer.DelRoad);
insert.Parameters.AddWithValue("@DelTown", customer.DelTown);
insert.Parameters.AddWithValue("@DelCity", customer.DelCity);
insert.Parameters.AddWithValue("@DelCounty", customer.DelCounty);
insert.Parameters.AddWithValue("@DelPostCode", customer.DelPostCode);
insert.Parameters.AddWithValue("@BilName", customer.BilName);
insert.Parameters.AddWithValue("@BilRoad", customer.BilRoad);
insert.Parameters.AddWithValue("@BilTown", customer.BilTown);
insert.Parameters.AddWithValue("@BilCity", customer.BilCity);
insert.Parameters.AddWithValue("@BilCounty", customer.BilCounty);
insert.Parameters.AddWithValue("@BilPostCode", customer.BilPostCode);
insert.ExecuteNonQuery();
Here is my table definition code:
CREATE TABLE [dbo].[orderDetails] (
[orderID] INT NOT NULL,
[Name] NCHAR (100) NULL,
[Phone] NCHAR (100) NULL,
[Mobile] NCHAR (15) NULL,
[Email] NCHAR (15) NULL,
[DelName] NCHAR (100) NULL,
[DelRoad] NCHAR (100) NULL,
[DelTown] NCHAR (100) NULL,
[DelCity] NCHAR (100) NULL,
[DelCounty] NCHAR (100) NULL,
[DelPostCode] NCHAR (100) NULL,
[BilName] NCHAR (100) NULL,
[BilRoad] NCHAR (100) NULL,
[BilTown] NCHAR (100) NULL,
[BilCity] NCHAR (100) NULL,
[BilCounty] NCHAR (100) NULL,
[BilPostCode] NCHAR (100) NULL,
PRIMARY KEY CLUSTERED ([orderID] ASC)
);
Customer Class
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string DelName { get; set; }
public string DelRoad { get; set; }
public string DelTown { get; set; }
public string DelCity { get; set; }
public string DelCounty { get; set; }
public string DelPostCode { get; set; }
public string BilName { get; set; }
public string BilRoad { get; set; }
public string BilTown { get; set; }
public string BilCity { get; set; }
public string BilCounty { get; set; }
public string BilPostCode { get; set; }
public bool sameasDel { get; set; }
}
Upvotes: 6
Views: 2170
Reputation: 5943
Whenever you see that error
string or binary data would be truncated error message
Just understand that you are trying to insert value into a field that cannot hold that value you are trying to insert
[Mobile] NCHAR (15) NULL,
[Email] NCHAR (15) NULL,
they can hold only 15 chars , check yourself if you are trying to insert more than that .
Upvotes: 6
Reputation: 8147
This message is shown when you are trying to insert some data that is too big for the field.
A prime candidate here is Email
- you only have it set as 15
character whereas most email addresses are going to be much bigger! Increase it to 255
and try again.
Check all of the others too, especially small ones like Mobile
.
Upvotes: 7