2017-10-22 4 views
0

gridview의 드롭 다운 목록에 액세스하여 선택한 값을 데이터베이스에 저장하려고하지만 개체 참조가 개체 인스턴스로 설정되지 않은 오류를 계속 표시하려고합니다..Net 개체 참조가 Dropdownlist 개체의 인스턴스로 설정되지 않았습니다.

void Gridview1_RowUpdating은 gridview 편집에서 데이터베이스로 값을 저장하는 이미지 버튼과 함께 사용됩니다.

드롭 다운 목록의 html ID는 gridview의 "DropDownList1"입니다. 대안이 있습니까?

C 번호 :

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    try 
    { 
     con2.Open(); 
     using (con2) 
     { 
      DropDownList RoomID = e.RowIndex.FindControl("DropDownList1") as DropDownList;     
      string query = "UPDATE Info SET [email protected],[email protected],[email protected],[email protected],[email protected] WHERE [email protected]"; 
      SqlCommand sqlCmd = new SqlCommand(query, con2); 
      sqlCmd.Parameters.AddWithValue("@Forename", (GridView1.Rows[e.RowIndex].FindControl("txtForeName") as TextBox).Text.Trim()); 
      sqlCmd.Parameters.AddWithValue("@LastName", (GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox).Text.Trim()); 
      sqlCmd.Parameters.AddWithValue("@Email", (GridView1.Rows[e.RowIndex].FindControl("txtEmail") as TextBox).Text.Trim()); 
      sqlCmd.Parameters.AddWithValue("@Phone", (GridView1.Rows[e.RowIndex].FindControl("txtPhone") as TextBox).Text.Trim()); 
      sqlCmd.Parameters.AddWithValue("@RoomName",Convert.ToInt32(RoomID.SelectedValue)); 
      sqlCmd.Parameters.AddWithValue("@ID",Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString())); 


      sqlCmd.ExecuteNonQuery(); 
      GridView1.EditIndex = -1; 
      PopulateGridView(); 
      lblSuccessMessage.Text = "Selected Record Updated!"; 
      lblErrorMessage.Text = ""; 
      con2.Close(); 
     } 


    } 
    catch (Exception ex) 
    { 

     lblSuccessMessage.Text = ""; 
     lblErrorMessage.Text = ex.Message; 

    } 
} 

답변

0

e.RowIndex 당신은 거기에있는 DropDownList를 찾을 거 아니에요, int입니다. 필요한 항목 :

DropDownList RoomID = GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList;