问题描述:

I've tried two sets of code for the update button and all of them failed. 1st error was about truncuated double and second was it can't connect to DB. Can someone tell me what's wrong?

1st set of code:

 void updateSection()

{

con.Close();

cmd = new MySqlCommand("update deliver_supplies set Del_SSupplier ='" + SuppNameCombo.Text + "' and Del_SName ='" + txtProdName.Text + "' and Del_SBrand ='" + txtProdBrand.Text + "' and Del_SQty ='" + txtQty.Text + "' and Del_SReceived ='" + DTPReceived.Text + "' where Del_sID ='" + lblSuppIDChecker.Text + "'", con.con);

con.Open();

reader = cmd.ExecuteReader();

}

2nd set of code:

void updateSection()

{

string cmdText = @"update deliver_assets

set Del_ASupplier [email protected]

Del_AName = @name,

Del_ABrand = @brand

Del_AQty = @qty

Del_AReceived = @recv

where Del_aID = @id";

using(MySqlConnection con = new MySqlConnection(.....))

using(MySqlCommand cmd = new MySqlCommand(cmdText, con))

{

cmd.Parameters.Add("@sup", MySqlDbType.VarChar).Value = SuppNameCombo.Text;

cmd.Parameters.Add("@name", MySqlDbType.VarChar).Value = txtProdName.Text;

cmd.Parameters.Add("@brand", MySqlDbType.VarChar).Value = txtProdBrand.Text;

cmd.Parameters.Add("@qty", MySqlDbType.VarChar).Value = Convert.ToDouble(txtQty.Text);

cmd.Parameters.Add("@recv", MySqlDbType.VarChar).Value = DTPReceived.Text;

cmd.Parameters.Add("@id", MySqlDbType.Int32).Value = Convert.ToInt32(lblAssetIDChecker.Text);

con.Open();

int rowsUpdated = cmd.ExecuteNonQuery();

if(rowUpdated > 0)

MessageBox.Show("Record updated");

}

}

网友答案:

Parameter for Qty appears to be a varchar - you are using a Double - check your datatypes. Which is it? Use the right one. Second error would appear that you have no valid SQLCommand. Use a basic SELECT statement to check your connection and then add the parameters one at a time.

相关阅读:
Top