在c和sql中更新qty数据网格

bq3bfh9z  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(191)

我从这条线上得到这样的信息:“
对象引用未设置为对象的示例“

//update tblproduct qty
connection.Open();
command=new SqlCommand("Update tblproduct set qty = "+int.Parse(dataGridView2.Rows[i].Cells[5].Value.ToString())+" WHERE pcode like '" +dataGridView2.Rows[i].Cells[3].Value.ToString() + "'",connection); 
command.ExecuteNonQuery();
connection.Close();

 private void btnSave_Click(object sender, EventArgs e)
 {
     try
     {
         if (dataGridView2.Rows.Count > 0)
         {
             if (MessageBox.Show("Are you sure you want to save this records?", "DZNPOS", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
             {
                  for (int i = 0; i < dataGridView2.Rows.Count; i++)
                  {
                      //update tblproduct qty
                      connection.Open();
                      command=new SqlCommand("Update tblproduct set qty = "+int.Parse(dataGridView2.Rows[i].Cells[5].Value.ToString())+" WHERE pcode like '" +dataGridView2.Rows[i].Cells[3].Value.ToString() + "'",connection); 
                      command.ExecuteNonQuery();
                      connection.Close();

                      //update tblStockin
                      connection.Open();
                      command = new SqlCommand("update tblStockin set qty = qty +" + int.Parse(dataGridView2.Rows[i].Cells[5].Value.ToString()) + ", status = 'Done' where id like '" + dataGridView2.Rows[i].Cells[1].Value.ToString() + "'", connection);
                      command.ExecuteNonQuery();
                      connection.Close();
                  }
                  Clear();
                  LoadStockin();
              }
          }
      }
      catch (Exception ex)
      {    
          connection.Close();
          MessageBox.Show(ex.Message, "DZN POS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
       }
  }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题