2012-10-22 31 views
0

我得到這個錯誤「必須聲明標量變量」Post_ID「,如果我嘗試更新我的表中的2個不同的ID。換句話說,我可以根據需要更新多次例如ID#25,但如果我嘗試更新ID#26,那麼我得到上面的錯誤,我的插入函數工作得很好,只有我的更新函數,請幫助,我感謝您的時間注意,DateKeyNames = ID。我只更新代碼:必須聲明標量變量「Post_ID」

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString); 
      SqlCommand cmd = new SqlCommand(); 

      cmd.CommandText = "UPDATE MyTable SET [email protected]_ID, [email protected], [email protected] WHERE [email protected]"; 

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

回答

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; 
} 

這PATT對於myTextBox22myTextBox33重複ern。

我建議這樣的邏輯,而不是:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString); 
SqlCommand cmd = new SqlCommand(); 

cmd.CommandText = "UPDATE MyTable SET [email protected]_ID, [email protected], [email protected] WHERE [email protected]"; 

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

UPDATE

添加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"); 

失敗了。

+0

皮特,感謝您的幫助。我已經嘗試了你的方法完全按照你顯示的方式,除了這一行:myTextBox33!= null,我改爲「myTextBox33 == null」,但它不更新字段,這意味着一切都保持不變。好事是我沒有收到錯誤,但沒有更新。我在這裏錯過了什麼?謝謝 – moe

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到的變量。這樣,無論點擊哪行,它都會使用該字段。