2014-04-27 1 views
0

내 응용 프로그램의 DataGridview가 있습니다. SqlCommand을 사용하여 datagridview 열 값을 업데이트하려고하는데 코드가 있지만 오류가 표시됩니다. 내가 어떻게 할 수 있니?C#의 SqlCommand를 사용하여 datagridview 셀 값을 업데이트하십시오.

private void EDIT(object sender, EventArgs e) 
{ 
    foreach (DataGridViewRow row in datagrid.Rows) 
    { 
     if (datagrid.Rows.Count > 0) 
     { 
      int nRowIndex = datagrid.Rows.Count - 2; 

      if (datagrid.Rows[nRowIndex].Cells[0].Value != null) 
      { 
       con.Open(); 
       SqlCommand cmd11 = new SqlCommand("update Purchasedetail set [email protected] ,[email protected],[email protected],[email protected],[email protected] where [email protected]_id, [email protected]_name", con); 

       cmd11.Parameters.AddWithValue("@product_id", row.Cells[0].Value); 
       cmd11.Parameters.AddWithValue("@product_name", row.Cells[1].Value); 
       cmd11.Parameters.AddWithValue("@qty", row.Cells[2].Value); 
       cmd11.Parameters.AddWithValue("@price", row.Cells[3].Value); 
       cmd11.Parameters.AddWithValue("@tax", row.Cells[4].Value); 
       cmd11.Parameters.AddWithValue("@discount", row.Cells[5].Value); 
       cmd11.Parameters.AddWithValue("@total", row.Cells[6].Value); 

       cmd11.ExecuteNonQuery(); 

       MessageBox.Show("Updated Successfully", "OUTPUT", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       return; 
      } 
     } 
    } 
    con.Close(); 
} 
+2

정확히 어느 줄에 오류가 무엇입니까? 'SqlConnection'이'Open'입니까? –

+0

이미 추가했습니다 – user3467599

답변

0

문제 1 :

여기 내 업데이트 버튼 코드 또한 현재 루프에서 제대로 SqlCommand 개체를 닫을 수 없습니다.

해결 방법 1 : 루프에서 실행하는 동안 당신은 제대로 SqlCommand 객체를 close해야합니다. 당신은 함수에서 반환하는 당신이 실제로 단 하나 (모든 행에 대해 명령을 실행되지 않도록 첫 번째 명령을 실행 한 후 :

당신은 using block {}

using(SqlCommand cmd11 = new SqlCommand("update command")) 
{ 

} 

문제 2로 묶어야합니다 행) .1 행만 업데이트하면됩니까?

해결 방법 2 : 당신의 긴장은 return 문을 제거해야 할 하나 개의 행을 갱신하는 경우

문제 3 : 업데이트가 성공적인 아닌지 어떠했는지 당신은 심지어 검사 성공 Messagewithout을 표시하고 있습니다 .

해결책 3 : 성공 메시지를 표시하기 전에 모든 행이 성공적으로 업데이트되었는지 확인해야합니다.

전체 코드 :

private void EDIT(object sender, EventArgs e) 
{ 
    con.Open(); 
    bool UpdateFailed = false; 
    foreach (DataGridViewRow row in datagrid.Rows) 
    { 
     if (datagrid.Rows.Count > 0) 
     { 
      int nRowIndex = datagrid.Rows.Count - 2; 

      if (datagrid.Rows[nRowIndex].Cells[0].Value != null) 
      { 

       using(SqlCommand cmd11 = new SqlCommand("update Purchasedetail set 
        [email protected] ,[email protected],[email protected],[email protected],[email protected] 
        where [email protected]_id, [email protected]_name", con)) 
       { 
       cmd11.Parameters.AddWithValue("@product_id", row.Cells[0].Value); 
       cmd11.Parameters.AddWithValue("@product_name", row.Cells[1].Value); 
       cmd11.Parameters.AddWithValue("@qty", row.Cells[2].Value); 
       cmd11.Parameters.AddWithValue("@price", row.Cells[3].Value); 
       cmd11.Parameters.AddWithValue("@tax", row.Cells[4].Value); 
       cmd11.Parameters.AddWithValue("@discount", row.Cells[5].Value); 
       cmd11.Parameters.AddWithValue("@total", row.Cells[6].Value); 

       if(!Convert.ToInt32(cmd11.ExecuteNonQuery()) > 0) 
        UpdateFailed = true; 
       } 


      } 
     } 
    } 
     if(!UpdateFailed) 
      MessageBox.Show("Updated Successfully", "OUTPUT", MessageBoxButtons.OK, 
                 MessageBoxIcon.Information); 
    con.Close(); 
} 
+0

그건 더 이상 그렇지 않습니다. OP가이 줄을 추가했지만 여전히 오류가 있습니다. –

+0

@ SonerGönül : 문제가 될 수 있기 전에 거기에 없었습니다. 어쨌든 고마워요. –

+0

이 코드는 좋지만 선택한 행만 업데이트하고 싶습니다. – user3467599

관련 문제