我收到错误消息如何解决此消息 [英] I got Error Mssage How to resolve thismessage

查看:87
本文介绍了我收到错误消息如何解决此消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我在dr.BeginEdit()中出错;
对象引用未设置为对象的实例."}

Dear All,

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

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

推荐答案

dr可能为null,这就是它引发NullReferenceException的原因.
dr is probably null that''s why it throws the NullReferenceException.

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


将语句从dr.BeginEdit()放入if条件,如下所示
Put the statements from dr.BeginEdit() into a if condition like below
if (dr != null)


尝试一下:
Try this one :
DataTable dt = new DataTable();
dt = _sampleData;
DataRow dr = dt.Rows.Find(QuestionNo);
dr.BeginEdit();


这篇关于我收到错误消息如何解决此消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆