Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 必须声明标量变量";Post“U ID”;_Asp.net_Asp.net Mvc - Fatal编程技术网

Asp.net 必须声明标量变量";Post“U ID”;

Asp.net 必须声明标量变量";Post“U ID”;,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,我得到这个错误“必须声明标量变量”Post_ID“如果我尝试更新表中的两个不同ID。换句话说,我可以根据需要多次更新,例如ID#25,但如果我尝试更新ID#26,则会出现上述错误。我的insert函数仅在我的update函数中工作正常。请。帮帮忙,谢谢你抽出时间。请注意,DateKeyNames=ID。以下是我的更新代码: SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect

我得到这个错误“必须声明标量变量”Post_ID“如果我尝试更新表中的两个不同ID。换句话说,我可以根据需要多次更新,例如ID#25,但如果我尝试更新ID#26,则会出现上述错误。我的insert函数仅在我的update函数中工作正常。请。帮帮忙,谢谢你抽出时间。请注意,DateKeyNames=ID。以下是我的更新代码:

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();

你的逻辑有点错误:

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
条件添加到
抛出新异常(“myTextBox11为空”)用于
myTextBox11
myTextBox22
myTextBox33

这将允许您查看以下两种情况:

GV_InlineEditing.Rows(0).FindControl("Date") as TextBox;
以及:


你失败了。

我总是这样做:

 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放入变量中。这样,无论单击哪一行,它都将使用该字段

皮特,谢谢你的帮助。我已经完全按照您显示的方式尝试了您的方法,除了这一行:myTextBox33!=null,我改为“myTextBox33==null”,但它不会更新字段,这意味着一切都保持不变。好的是,我没有得到错误,但没有做更新。我错过了什么?谢谢
 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;