2012-07-11 4 views
3

У меня возникли проблемы с методом RowUpdating. Мой GridView подключен к нашему локальному SQL Server, и я пытаюсь обновить данные. Вот код для метода RowUpdating из MSDN.ASP.NET RowUpdating GridView Troubles

protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
     //Retrieve the table from the session object. 
     DataTable dt = (DataTable)Session["TaskTable"]; 

     //Update the values. 
     GridViewRow row = GridView1.Rows[e.RowIndex]; 
     dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text; 
     dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text; 
     dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked; 

     //Reset the edit index. 
     GridView1.EditIndex = -1; 

     //Bind data to the GridView control. 
     BindData(); 

} 

Я получаю эту ошибку:

System.NullReferenceException: Object reference not set to an instance of an object.

+0

Вы получаете доступ так много элементов без нулевых проверок, что это может быть любое количество вещей. Какая строка бросает исключение? Вы уверены, что все элементы управления существуют в ячейках, которые вы указываете? Вы уверены, что сами клетки существуют? –

ответ

3

попробовать этот код только один раз.

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 tname = (TextBox)row.FindControl("nam"); 
    TextBox tques = (TextBox)row.FindControl("que"); 
    MySqlCommand cmd = new MySqlCommand("update exam set [email protected],[email protected] where id = @id", con); 
    cmd.Parameters.Add("@id", MySqlDbType.Int16).Value = id; 
    cmd.Parameters.Add("@name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim(); 
    cmd.Parameters.Add("@ques", MySqlDbType.VarChar,40).Value = tques.Text.Trim(); 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
    GridView1.EditIndex = -1; 
    bind(); 
} 
2
public void bindGvEdit() 
{ 
    GridView1.DataSource = obj1.SelectAlltbl(); 
    GridView1.DataBind(); 
} 

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    GridView1.EditIndex = e.NewEditIndex; 
    bindGvEdit(); 
} 

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
{ 
    GridView1.EditIndex = -1; 
    bindGvEdit(); 
} 

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    GridViewRow row = GridView1.Rows[e.RowIndex]; 
    obj1.Id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value); 
    obj1.Name = ((TextBox)row.Cells[1].Controls[1]).Text; 
    obj1.Description = ((TextBox)row.Cells[2].Controls[1]).Text; 
    obj1.Updatetbl(); 
    GridView1.EditIndex = -1; 
    bindGvEdit(); 
} 
+0

Добро пожаловать в StackOverflow! В ваших следующих сообщениях убедитесь, что ваше форматирование кода верное. – Dariusz

Смежные вопросы