Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I Got Error In dr.BeginEdit();
"Object reference not set to an instance of an object."}

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridView _gridView = (GridView)sender;

        if (e.RowIndex > -1)
        {
            // Loop though the columns to find a cell in edit mode
            for (int i = _firstEditCellIndex; i < _gridView.Columns.Count; i++)
            {
                // Get the editing control for the cell
                Control _editControl = _gridView.Rows[e.RowIndex].Cells[i].Controls[0];
                if (_editControl.Visible)
                {
                    int _dataTableColumnIndex = i - 1;

                    try
                    {
                        // Get the id of the row
                        Label idLabel = (Label)_gridView.Rows[e.RowIndex].FindControl("lblMessage");
                        String QuestionNo = (lblMessage.Text).ToString();
                        // Get the value of the edit control and update the DataTable
                        DataTable dt = _sampleData;
                        DataRow dr = dt.Rows.Find(QuestionNo);
                        dr.BeginEdit();
                        if (_editControl is TextBox)
                        {
                            dr[_dataTableColumnIndex] = ((TextBox)_editControl).Text;
                        }
                        //else if (_editControl is DropDownList)
                        //{
                        //    dr[_dataTableColumnIndex] = ((DropDownList)_editControl).SelectedValue;
                        //}
                        //else if (_editControl is CheckBox)
                        //{
                        //    dr[_dataTableColumnIndex] = ((CheckBox)_editControl).Checked;
                        //}
                        dr.EndEdit();

                        // Save the updated DataTable
                        _sampleData = dt;

                        // Clear the selected index to prevent 
                        // another update on the next postback
                        _gridView.SelectedIndex = -1;

                        // Repopulate the GridView
                        _gridView.DataSource = dt;
                        _gridView.DataBind();
                    }
                    catch (ArgumentException)
                    {
                        this.lblMessage.Text += "Error updating GridView row at index " + e.RowIndex + "<br />";

                        // Repopulate the GridView
                        _gridView.DataSource = _sampleData;
                        _gridView.DataBind();
                    }
                }
            }
        }
    }


Thank u for Advance valuable reply
Posted
Updated 30-Nov-11 21:48pm
v4
Comments
[no name] 1-Dec-11 1:18am    
EDIT: removed "pre" tag

dr is probably null that's why it throws the NullReferenceException.

C#
DataRow dr = dt.Rows.Find(QuestionNo); //this probably returns null

if(Equals(dr, null))
{
  //do whatever you want. or continue the loop
  continue;
}

dr.BeginEdit();
 
Share this answer
 
Put the statements from dr.BeginEdit() into a if condition like below
if (dr != null)
 
Share this answer
 
v2
Try this one :
C#
DataTable dt = new DataTable();
dt = _sampleData;
DataRow dr = dt.Rows.Find(QuestionNo);
dr.BeginEdit();
 
Share this answer
 
Comments
2011999 1-Dec-11 1:15am    
Same Error Message display

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900