使用行索引值更新SQL

时间:2017-06-13 15:07:03

标签: c# sql asp.net gridview

我有一个GridView,我想编辑SQL值。出于某种原因,我无法像我想的那样得到价值。这是我尝试过的第一个无法工作的代码:

String acctNum = customerLookup.Rows[e.RowIndex].Cells[2].Text;
cn2.Open();
SqlCommand cmd = new SqlCommand("Update custInfo set acctNum='"+acctNum+"' WHERE customerName='Strathman,Tim'", cn2);

这没有按照我想要的值传递,所以我想我会尝试使用这样的参数值:

SqlCommand cmd = new SqlCommand("update custInfo set acctNum = @acctNum where customerName = @customerName3", cn2);

    cmd.Parameters.AddWithValue("@customerName3", customerDropDown.Text.Trim());
    cmd.Parameters.AddWithValue("@acctNum", customerLookup.Rows[e.RowIndex].Cells[3].Text);
    cmd.ExecuteNonQuery();

这也不起作用......当我为账号输入类似'1234'的手动输入时,它确实有效,所以我知道这不是customerName参数的问题......任何线索都是如此我错过的将是伟大的

1 个答案:

答案 0 :(得分:0)

想出来。这非常有效:

GridViewRow row = (GridViewRow)customerLookup.Rows[e.RowIndex];
string acctNum = ((TextBox)row.Cells[1].Controls[0]).Text;