开发者

System.Data.SqlClient.Exception ( 0x80131904) String or binary would be terminated

开发者 https://www.devze.com 2023-01-27 18:55 出处:网络
i have the following code and when i execute it i get the following error : System.Data.SqlClient.Exception ( 开发者_运维技巧0x80131904) String or binary would be terminated

i have the following code and when i execute it i get the following error : System.Data.SqlClient.Exception ( 开发者_运维技巧0x80131904) String or binary would be terminated

form1.con.Open();
command = new SqlCommand("UPDATE sucursale SET nume_sucursala='" + textBox1.Text.Trim() + "',sucursala_coord='" + textBox3.Text.Trim() + "',mida='" + textBox4.Text.Trim() + "',flex='" + textBox5.Text.Trim() + "',nr_sucursala='" + textBox6.Text.Trim() + "',director='" + textBox7.Text.Trim() + "',tel_director='" + textBox8 + "',director_adj='" + textBox9.Text.Trim() + "',tel_director_adj='" + textBox10.Text.Trim() + "',adresa='" + textBox14.Text.Trim() + "',telefon='" + textBox15.Text.Trim() + "',fax='" + textBox16.Text.Trim() + "',u1_days='" + textBox26.Text.Trim() + "',u1_company='" + textBox17.Text.Trim() + "',u2_days='" + textBox27.Text.Trim() + "',u2_company='" + textBox18.Text.Trim() + "',u3_days='" + textBox28.Text.Trim() + "',u3_company='" + textBox19.Text.Trim() + "',u4_days='" + textBox29.Text.Trim() + "',u4_company='" + textBox20.Text.Trim() + "',u5_days='" + textBox30.Text.Trim() + "',u5_company='" + textBox21.Text.Trim() + "',u6_days='" + textBox31.Text.Trim() + "',u6_company='" + textBox22.Text.Trim() + "',retea_interna='" + textBox23.Text.Trim() + "',timer='" + textBox24.Text.Trim() + "',program_clienti='" + textBox25.Text.Trim() + "',contact_extra='" + textBox11.Text.Trim() + "',functie_contact_extra='" + textBox12.Text.Trim() + "',telefon_contact_extra='" + textBox13.Text.Trim() + "' WHERE nr_sucursala = '" + comboBox3.SelectedItem + "'", form1.con);
form1.da2.UpdateCommand = command;

form1.da2.Update(form1.ds2, "sucursale");
form1.con.Close();


Firstly; don't concatenate user input; that should be parameterized:

using(var cmd = conn.CreateCommand()) {
    cmd.CommandText = @"
UPDATE [SomeTable]
SET [SomeColumn] = @someColumn
WHERE [Id] = @id";
    cmd.Parameters.AddWithValue("someColumn", usersNastyText);
    cmd.Parameters.AddWithValue("id", recordId);
    cmd.ExecuteNonQuery();
}

The error means that the text in one of the boxes is longer than the declared column. Either expand the column in the database, or validate the length of the text in the boxes before sending to the server.

Obligatory XKCD reference

System.Data.SqlClient.Exception ( 0x80131904) String or binary would be terminated

0

精彩评论

暂无评论...
验证码 换一张
取 消