Reputation: 29
I'm trying to update a column with a string and it doesn't update correctly.
I'm displaying the string
right before the update function and even after it's passed as a parameter (tip_reparatii
,from the function) it's the same string but it doesn't update in the database.
The update function:
public void updateInventar ( int ID_INVENTAR, int ID_CEAS, DateTime data_intrare, string tip_reparatii, string pret, int tip_contact1, int ok_refuse, int tip_contact2, DateTime ok_refuse_date, int ID_STATUS, int ID_LOCALIZARE, DateTime data_iesire, string note, int ID_TEHNICIAN ) {
if ( IsOnline() ) {
try {
string sqlInsertQuery = "UPDATE dbo.Inventar SET data_intrare=@data_intrare, tip_reparatii=@tip_reparatii, pret=@pret, tip_contact1=@tip_contact1, ok_refuse=@ok_refuse, tip_contact2=@tip_contact2, ok_refuse_date=@ok_refuse_date, ID_STATUS=@ID_STATUS, ID_LOCALIZARE=@ID_LOCALIZARE, data_iesire=@data_iesire, note=@note,ID_TEHNICIAN=@ID_TEHNICIAN WHERE ID_INVENTAR=@ID_INVENTAR AND ID_CEAS=@ID_CEAS";
SqlCommand cmd = new SqlCommand(sqlInsertQuery, DbCon);
MessageBox.Show(tip_reparatii); //this is the same string and it's correct
cmd.Parameters.AddWithValue("@data_intrare", data_intrare);
cmd.Parameters.AddWithValue("@tip_reparatii", tip_reparatii);
cmd.Parameters.AddWithValue("@pret", pret);
cmd.Parameters.AddWithValue("@tip_contact1", tip_contact1);
cmd.Parameters.AddWithValue("@ok_refuse", ok_refuse);
cmd.Parameters.AddWithValue("@tip_contact2", tip_contact2);
cmd.Parameters.AddWithValue("@ok_refuse_date", ok_refuse_date);
cmd.Parameters.AddWithValue("@ID_STATUS", ID_STATUS);
cmd.Parameters.AddWithValue("@ID_LOCALIZARE", ID_LOCALIZARE);
cmd.Parameters.AddWithValue("@data_iesire", data_iesire);
cmd.Parameters.AddWithValue("@note", note);
cmd.Parameters.AddWithValue("@ID_TEHNICIAN", ID_TEHNICIAN);
cmd.Parameters.AddWithValue("@ID_INVENTAR", ID_INVENTAR);
cmd.Parameters.AddWithValue("@ID_CEAS", ID_CEAS);
cmd.ExecuteNonQuery();
} catch ( SqlException e ) {
MessageBox.Show(null, "Could not Update! " + e.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} else {
MessageBox.Show(null, "SQL Connection Throttled!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
The call code:
private void btnActualiser_Click(object sender, EventArgs e)
{
string tipReparatiiString = "";
if (this.cblReparatii.CheckedItems.Count > 0)
{
foreach (DataRowView itm in cblReparatii.CheckedItems)
{
tipReparatiiString += itm.Row.ItemArray[0].ToString() + " ";
}
MessageBox.Show(tipReparatiiString); //this is correct
obj.updateInventar(int.Parse(this.cblReparatii.SelectedValue.ToString()), int.Parse(this.cbCeas.SelectedValue.ToString()), this.dtDataIn.Value, tipReparatiiString, this.tbPrix.Text, this.cbPrin.SelectedIndex, this.cbOkRef.SelectedIndex, this.cbPrin2.SelectedIndex, this.dtOkRef.Value, int.Parse(this.cbStatus.SelectedValue.ToString()), int.Parse(this.cbLocatie.SelectedValue.ToString()), this.dtDataOut.Value, this.tbNote.Text, int.Parse(this.cbTehnician.SelectedValue.ToString()));
}
else
{
//Msg
}
}
Both message boxes display the correct string but it doesn't update in the database or it sometimes updates incorrectly(a piece of the string).
We added a string
manually as a parameter and it worked:
obj.updateInventar(int.Parse(this.cblReparatii.SelectedValue.ToString()),
int.Parse(this.cbCeas.SelectedValue.ToString()),
this.dtDataIn.Value, "this works perfectly",
this.tbPrix.Text, this.cbPrin.SelectedIndex,
this.cbOkRef.SelectedIndex, this.cbPrin2.SelectedIndex,
this.dtOkRef.Value,
int.Parse(this.cbStatus.SelectedValue.ToString()),
int.Parse(this.cbLocatie.SelectedValue.ToString()),
this.dtDataOut.Value, this.tbNote.Text,
int.Parse(this.cbTehnician.SelectedValue.ToString()) );
The other values update correctly.
Upvotes: 0
Views: 196
Reputation: 86
Instead of using cmd.Parameters.AddWithValue("@tip_reparatii", tip_reparatii);
Try:
cmd.Parameters.Add("@tip_reparatii",SqlDbType.NVarChar).Value=tip_reparatii;
or with a size parameter such as cmd.Parameters.Add("@tip_reparatii",SqlDbType.NVarChar,200).Value=tip_reparatii;
You should do this type of add for all your SQL command Parameters.
Upvotes: 1