0

テーブル内の 2 つの異なる ID を更新しようとすると、「スカラー変数 "Post_ID" を宣言する必要があります」というエラーが表示されます。つまり、ID # 25 など、必要な回数だけ更新できますが、 ID # 26 を更新すると、上記のエラーが表示されます。挿入機能は、更新機能のみで正常に動作します。お時間をいただき、ありがとうございます。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();
4

2 に答える 2

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");追加します。myTextBox11myTextBox22myTextBox33

これにより、次の両方かどうかを確認できます。

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

と:

GV_InlineEditing.Rows(0).Cells(0).FindControl("GV_Post_ID");

失敗しています。

于 2012-10-22T22:40:39.713 に答える
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 を変数に入れる必要があります。そうすれば、どの行がクリックされても、そのフィールドが使用されます。

于 2012-10-22T23:12:03.817 に答える