2012-10-22 2 views
0

"테이블에 2 개의 다른 ID를 업데이트하려고하면 스칼라 변수를 선언해야합니다."즉, 필요한만큼 여러 번 업데이트 할 수 있습니다 예를 들어 ID # 25하지만 ID 26을 업데이트하려고하면 위의 오류가 발생합니다. 내 삽입 함수는 내 업데이트 기능 만 제대로 작동합니다 .Pls.help 및 시간 주셔서 감사합니다 .DetKeyNames = 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; 
} 

이 패티 반복은 myTextBox22myTextBox33에 대해 반복됩니다.

내가 대신이 논리를 제안 :

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

myTextBox11, myTextBox22myTextBox33에 대한 Throw New Exception("myTextBox11 is null");else 조건을 추가합니다.

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

가 실패 :

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

과 : 두 경우에 당신이 볼 수

.

+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를 얻을 수 있습니다. 그런 식으로 어떤 행이 클릭 되더라도 그 필드를 사용할 것입니다.

관련 문제