Chidiebere Mgbemena

Chidiebere Mgbemena

  • 1.8k
  • 179
  • 4.9k

Error in save update

Jun 7 2020 9:32 AM
When i tried to save my update, i got this error message."ExecuteNonQuery: Command text property has not been initialized.see below code and screen shot.What code can I add and where?private void btnSave_Click(object sender, EventArgs e) { // delete old bills details from Row data try { con.Open(); cmd = new SqlCommand("Delete from Tbl_RowData where JobNo = '" + txtJobNo.Text + "'", con); cmd.ExecuteNonQuery(); con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } // now save updated Bill data try { for(int i = 0; i < dataGridView1.Rows.Count; i++) { SqlCommand cmd1 = new SqlCommand("Insert into Tbl_RowData(SINO,Customer,Description,Price,Quantity,Value,JobNo,Date)values('" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[3].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[4].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[5].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[6].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[7].Value.ToString() + "')", con); con.Open(); cmd1.Connection = con; cmd1.CommandType = CommandType.Text; cmd1.ExecuteNonQuery(); con.Close(); MessageBox.Show("Bill Updated"); } } catch(Exception ex) { MessageBox.Show(ex.Message); } // now update bill amount, discount and final amount try { con.Open(); SqlCommand cmd = new SqlCommand(query, con); //Pass values to Parameters cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value.ToString("dd/MM/yyyy")); cmd.Parameters.AddWithValue("@GoodValue", txtTotalGoodValue.Text); cmd.Parameters.AddWithValue("@Vat", txtVat.Text); cmd.Parameters.AddWithValue("@StampDuty", txtStampDuty.Text); cmd.Parameters.AddWithValue("@DeliveryCharges", txtDeliveryCharges.Text); cmd.Parameters.AddWithValue("@OtherCharges", txtOtherCharges.Text); cmd.Parameters.AddWithValue("@Tota", txtTotal.Text); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Bill Updated"); } catch(Exception ex) { MessageBox.Show(ex.Message); } } } }

Brought to you by:

Answers (6)