Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

am trying to insert data in database.

its inserting the data in database table.

but it's throwing Exception SqlException was unhandeled.(String or binary data would be truncated. The statement has been terminated.)

and when every am clicking insert button its inserting data repetaedly.

can anyone help me in solving this query.

C#
ServiceController[] services = ServiceController.GetServices();
{
              dataGridView1.DataSource = services ;
}



 string conn = "Data Source=HostName;Initial Catalog=MSNETDB;Integrated Security=True;Pooling=False";
            SqlConnection mycon = new SqlConnection(conn);
            mycon.Open();
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    
                    string Displayname = row.Cells[3].Value.ToString();
                    string ServiceName = row.Cells[4].Value.ToString();
                    string Status = row.Cells[5].Value.ToString();
                    string ServiceType = row.Cells[6].Value.ToString();              
                    
    
                    SqlCommand com = new SqlCommand(conn, mycon);
                   
                    com .CommandText = "insert into SystemServiceInfo(DisplayName,ServiceName,Status,ServiceType)values('" + Displayname + "','" + ServiceName + "','" + Status + "','" + ServiceType + "')";
                    
                   com.ExecuteNonQuery();
                    com.Parameters.Clear();
                    com.Dispose();


                }
            
                mycon.Close();
                mycon.Dispose();


Thanks & Regards
sam.198979
Posted

It seems the any of the four columns on which you are working have maxlength less than what you are inserting actually.
Have you checked the inserted data ? do you mean that the data is actually inserted even after this exception ?
 
Share this answer
 
Please check the Data Length of the Fields
Quote:
DisplayName,ServiceName,Status,ServiceType
in Database and make sure the data you pass does not exceeds the given length.


If needed make changes accordingly in the Datatable Fields.
 
Share this answer
 

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