我收到此错误“必须声明标量变量”Post_ID“如果我尝试在我的表中更新2个不同的ID。换句话说,我可以更新我需要的次数,例如ID#25但是如果我尝试更新ID#26然后我得到上面的错误。我的插入功能只能正常我的更新功能。请帮助,我感谢您的时间。注意,DateKeyNames = ID。这是我的更新代码:< / p>
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE MyTable SET Post_ID=@Post_ID, Date=@Date, Description=@Description WHERE ID=@ID";
TextBox myTextBox11 = GV_InlineEditing.Rows[0].FindControl("GV_Post_ID") as TextBox;
TextBox myTextBox22 = GV_InlineEditing.Rows[0].FindControl("TextBox2") as TextBox;
TextBox myTextBox33 = GV_InlineEditing.Rows[0].FindControl("TextBox3") as TextBox;
if (myTextBox11 != null)
{
cmd.Parameters.Add("@Post_ID", SqlDbType.VarChar).Value = myTextBox11.Text;
}
else
{
TextBox GV_Post_ID = GV_InlineEditing.Rows[0].Cells[0].FindControl("GV_Post_ID") as TextBox;
}
if (myTextBox22 != null)
{
cmd.Parameters.Add("@Date", SqlDbType.VarChar).Value = myTextBox22.Text;
}
else
{
TextBox GV_Post_ID = GV_InlineEditing.Rows[0].Cells[0].FindControl("Date") as TextBox;
}
if (myTextBox33 != null)
{
cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = myTextBox33.Text;
}
else
{
TextBox GV_Post_ID = GV_InlineEditing.Rows[0].Cells[0].FindControl("Description") as TextBox;
}
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(GV_InlineEditing.Rows[e.RowIndex].Cells[1].Text);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
GV_InlineEditing.EditIndex = -1;
BindData();
答案 0 :(得分:1)
你的逻辑有些错误:
if (myTextBox11 != null)
{
//add paramter
cmd.Parameters.Add("@Post_ID", SqlDbType.VarChar).Value = myTextBox11.Text;
}
else
{
//declare a different textbox and do not add the SQL parameter
TextBox GV_Post_ID = GV_InlineEditing.Rows[0].Cells[0].FindControl("GV_Post_ID") as TextBox;
}
myTextBox22
和myTextBox33
重复此模式。
我会建议这个逻辑:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE MyTable SET Post_ID=@Post_ID, Date=@Date, Description=@Description WHERE ID=@ID";
TextBox myTextBox11 = GV_InlineEditing.Rows(0).FindControl("GV_Post_ID") as TextBox;
TextBox myTextBox22 = GV_InlineEditing.Rows(0).FindControl("Date") as TextBox;
TextBox myTextBox33 = GV_InlineEditing.Rows(0).FindControl("Description") as TextBox;
if (myTextBox11 == null) {
//try an alternate control for myTextBox11
myTextBox11 = GV_InlineEditing.Rows(0).Cells(0).FindControl("GV_Post_ID") as TextBox;
}
if (myTextBox22 == null) {
//try an alternate control for myTextBox22
myTextBox22 = GV_InlineEditing.Rows(0).Cells(0).FindControl("Date") as TextBox;
}
if (myTextBox33 == null) {
//try an alternate control for myTextBox33
myTextBox33 = GV_InlineEditing.Rows(0).Cells(0).FindControl("Description") as TextBox;
}
if (myTextBox11 != null & myTextBox22 != null & myTextBox33 != null) {
//all three textbox controls found
cmd.Parameters.Add("@Post_ID", SqlDbType.VarChar).Value = myTextBox11.Text;
cmd.Parameters.Add("@Date", SqlDbType.VarChar).Value = myTextBox22.Text;
cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = myTextBox33.Text;
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(GV_InlineEditing.Rows(e.RowIndex).Cells(1).Text);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
<强>更新强>
为else
,Throw New Exception("myTextBox11 is null");
和myTextBox11
myTextBox22
添加myTextBox33
条件。
这将允许您查看是否:
GV_InlineEditing.Rows(0).FindControl("Date") as TextBox;
和
GV_InlineEditing.Rows(0).Cells(0).FindControl("GV_Post_ID");
失败了。
答案 1 :(得分:0)
我总是这样做:
protected void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
TextBox date = (TextBox)row.FindControl("date");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "update t_your_table " +
"set " +
"date = @date, " +
" time = @time where id = @ID "
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;
cmd.Parameters.Add("@date", SqlDbType.VarChar).Value = date.Text;
您必须将datakeyname放入变量中。这样,无论点击哪一行,它都将使用该字段。