Click here to Skip to main content
16,019,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Everytime when I run the program it says

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: String or binary data would be truncated.

The statement has been terminated.

Please keep it simple in the solutions, i'm kinda new to C#.

What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
{
 con = new SqlConnection("The Database connection string")
 con.Open();
 cmd = new SqlCommand("INSERT INTO Storage (Serial,Name) VALUES (@Serial,@Name)",     con);
 cmd.Parameters.Add("@Serial", txtSerial.Text);
 cmd.Parameters.Add("@Name", txtName.Text);
 cmd.ExecuteNonQuery();
}
Posted
Updated 3-Jan-17 6:14am
Comments
[no name] 3-Jan-17 11:11am    
Do you know how to use google? You are trying to stuff more information into a table column than it is setup to handle.

Hi, it could be that at least one of Serial or Name has length > database table column.
 
Share this answer
 
Literally as the error message said: you are trying to insert a string or binary data that is longer than the max length set for the target field. You have to find out which field is the culprit. To resolve this, you may change the max length allowed for that field or limit the length of the input data through validation, depending on your requirements.
 
Share this answer
 
v3
try
           {
               using (SqlConnection connection = new SqlConnection(connectionString))
               {
                   using (SqlCommand cmd = new SqlCommand("INSERT INTO Storage (Serial,Name) VALUES (@Serial,@Name)", con))
                   {
                       connection.Open();
                       cmd.Parameters.Clear();
                       cmd.Parameters.Add("@Serial", txtSerial.Text);
                       cmd.Parameters.Add("@Name", txtName.Text);
                       int res=cmd.ExecuteNonQuery();
                       if (res > 0)
                       {
                           MessageBox.Show("Done");
                       }
                       else
                       {
                           MessageBox.Show("Error");
                       }

                       connection.Close();
                   }

               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }


please use sql quary after connection variable name like this


using (SqlCommand cmd = new SqlCommand("INSERT INTO Storage (Serial,Name) VALUES (@Serial,@Name)", ******** /*this place*/))
 
Share this answer
 
Comments
[no name] 3-Jan-17 14:49pm    
Perhaps you could explain how this code solves the OPs error that is on the DATABASE side of things and has nothing to do with the C# code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900