0

When I try Click button save it displays the following error

Error

String or binary data would be truncated.

Code:

 var connectionString = new ConnectionString();

var conn = new SqlConnection(connectionString.ViMS_LOCAL());
string query = "INSERT INTO  [Visitor.Profile] 
    (Name,NRIC,Address,StartVisit,Gender,VisitorType,PassNumber,PlateNumber,StatusId, Reason) VALUES `
    (@Name,@NRIC,@Address,@StartVisit,@Gender,@VisitorType,@PassNumber,@PlateNumber,@StatusId, @Reason)";`

SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@Name", VisitorName.Text);
cmd.Parameters.AddWithValue("@NRIC", NRIC.Text);
cmd.Parameters.AddWithValue("@Address", Address.Text);
cmd.Parameters.AddWithValue("@StartVisit", sd1.Text);

//Radiobutton 
if (Male.IsChecked == true) {
    cmd.Parameters.AddWithValue("@Gender", Male.Content);
}
if (Female.IsChecked == true) {
    cmd.Parameters.AddWithValue("@Gender", Female.Content);
}    
if (Ped.IsChecked == true) {
    cmd.Parameters.AddWithValue("@VisitorType", Ped.Content);
}
if (Veh.IsChecked == true) {
    cmd.Parameters.AddWithValue("@VisitorType", Veh.Content);
}

cmd.Parameters.AddWithValue("@PassNumber", PassNumber.Text);
cmd.Parameters.AddWithValue("@PlateNumber", PlateNumber.Text);

if (RadEnab1.IsChecked == true) {
    cmd.Parameters.AddWithValue("@StatusId", RadEnab1.Content);
}
if (RadDis1.IsChecked == true) {
    cmd.Parameters.AddWithValue("@StatusId", RadDis1.Content);
}

` //Combobox function`    
if (Reason.Text == "Meeting") {
    cmd.Parameters.AddWithValue("@Reason", Reason.Text);
}
if (Reason.Text == "Delivery") {
    cmd.Parameters.AddWithValue("@Reason", Reason.Text);
}
if (Reason.Text == "Pickup") {
    cmd.Parameters.AddWithValue("@Reason", Reason.Text);
}


try {
    conn.Open();
    cmd.ExecuteNonQuery();

} catch (Exception err) {

} finally {
    conn.Close();
}
vSuccess.Content = "* Visitor Register Success!";
//}
}

I try debug it,everything it ok,but when it go to "catch (Exception err)"it display error

How to solve this problem?

1
  • Check if the size of para meter in stored procedure is less than the size of data being passed? Commented Feb 18, 2015 at 6:56

1 Answer 1

1

Compare the lengths of the Text you are passing to the command and the size of the column in DataBase.

If the length of the text you are passing is greater than the specified field size in database this error occurs.

Example : In my Table-

FieldName Size

Name nVarchar(10)

If You pass a name with length greater than 10 this error occurs.

Hope this helps. I am new to c# and I got this exception recently.

Sign up to request clarification or add additional context in comments.

1 Comment

It work,I never realize that my StatusId is nvarchar(2) in database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.