dataGridView だけでなく、dataBase も編集したいと考えています。dataGridview を編集した後にクリックすると、データベースが更新されるボタンがあります。私の最初の行は更新されませんが、他の行は更新されません。ここに私のコードがあります
private void button2_Click(object sender, EventArgs e)
{
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string id = row.Cells["Product ID"].Value.ToString();
string name = row.Cells["Product Name"].Value.ToString();
string description = row.Cells["Product Description"].Value.ToString();
string category = row.Cells["Category"].Value.ToString();
string quantity = row.Cells["QTY"].Value.ToString();
string buyPrice = row.Cells["Buy Price"].Value.ToString();
string sellPrice = row.Cells["Sell Price"].Value.ToString();
string date = row.Cells["Date"].Value.ToString();
String query = "UPDATE [Stock List Table3] SET [Product Name] = '" + name + "',[Product Description] = '" + description + "',[Category] = '" + category + "',[QTY] = '" + quantity + "',[Buy Price] = '" + buyPrice + "',[Sell Price] = '" + sellPrice + "',Date = '" + date + "' WHERE [Product ID] = '" + id + "'";
m.Update_By_DataGridView_Information(query);
m.Load_Table1(dataGridView1);
m.Load_Table1(dataGridView4);
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
public void Load_Table1(DataGridView dv)
{
string query = "SELECT * FROM [Stock List Table3]";
SqlConnection Conn = create_connection();
SqlCommand cmd = new SqlCommand(query, Conn);
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dataset = new DataTable();
sda.Fill(dataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dataset;
dv.DataSource = bSource;
sda.Update(dataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Update_By_DataGridView_Information(string query)
{
try
{
SqlConnection Conn = create_connection();
SqlCommand cmd = new SqlCommand(query, Conn);
cmd.ExecuteNonQuery();
Conn.Close();
//MessageBox.Show("Updated Product Information Successfully", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
これが私のコードの合計です Error Line catch block in button2