我的gridview中的“对象引用未设置为对象的实例”

Posted

技术标签:

【中文标题】我的gridview中的“对象引用未设置为对象的实例”【英文标题】:"Object reference not set to an instance of an object" in my gridview 【发布时间】:2017-12-28 12:20:16 【问题描述】:

我该怎么办...我通过搜索谷歌尝试了很多代码但失败了...请帮助我

protected void RegistrationDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
        int stdid = Convert.ToInt32(RegistrationDetails.DataKeys[e.RowIndex].Value.ToString());
        int stdroll = Convert.ToInt32(RegistrationDetails.DataKeys[e.RowIndex].Value.ToString());
        int clsid = Convert.ToInt32(RegistrationDetails.DataKeys[e.RowIndex].Value.ToString());
        //DateTime tdob = Convert.ToDateTime(RegistrationDetails.DataKeys[e.RowIndex].Value);
        int vyear = Convert.ToInt32(RegistrationDetails.DataKeys[e.RowIndex].Value.ToString());
        //DateTime vadmdate = Convert.ToDateTime(RegistrationDetails.DataKeys[e.RowIndex].Value);
        GridViewRow row = (GridViewRow)RegistrationDetails.Rows[e.RowIndex]; 
        Label lblID = (Label)RegistrationDetails.Rows[e.RowIndex].FindControl("lblID");
        //TextBox txtname=(TextBox)gr.cell[].control[];  
        //TextBox tid = RegistrationDetails.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox;
        //TextBox troll = RegistrationDetails.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox;
        TextBox tname = RegistrationDetails.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox;
        TextBox tdob = RegistrationDetails.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox;
        TextBox taddress = RegistrationDetails.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox;
        TextBox tcontactp = RegistrationDetails.Rows[e.RowIndex].Cells[6].Controls[0] as TextBox;
        TextBox tgender = RegistrationDetails.Rows[e.RowIndex].Cells[7].Controls[0] as TextBox;
        TextBox tmobile = RegistrationDetails.Rows[e.RowIndex].Cells[8].Controls[0] as TextBox;
        //TextBox tyear = RegistrationDetails.Rows[e.RowIndex].Cells[8].Controls[0] as TextBox;
        TextBox tjdate = RegistrationDetails.Rows[e.RowIndex].Cells[10].Controls[0] as TextBox;
        TextBox tpass = RegistrationDetails.Rows[e.RowIndex].Cells[11].Controls[0] as TextBox;
        //TextBox tclsid = RegistrationDetails.Rows[e.RowIndex].Cells[11].Controls[0] as TextBox;
        TextBox tsecid = RegistrationDetails.Rows[e.RowIndex].Cells[12].Controls[0] as TextBox;

        //string vfullname = fullname.Text;
        //string vaddress = address.Text;
        //string vcperson = cperson.Text;
        //string vmobile = mobile.Text;
        //string vpass = pass.Text;
        //TextBox textadd = (TextBox)row.FindControl("txtadd");  
        //TextBox textc = (TextBox)row.FindControl("txtc");  
        RegistrationDetails.EditIndex = -1;
        con.Open();
        //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);  
        OleDbCommand cmd = new OleDbCommand("update registration set Roll=" + stdroll + ", Full_Name='" + tname.Text + "', Date_of_Birth='" + tdob.Text + "',Address='" + taddress.Text.Trim() + "', Contact_Person='" + tcontactp.Text + "', Gender='" + tgender.Text + "', Mobile='" + tmobile.Text + "', CYear=" + vyear + ", Joining_Date='" + tjdate.Text + "', UPassword='" + tpass.Text + "', Class_ID=" + clsid + ", Section_ID='" + tsecid.Text + "' where Student_ID=" + stdid + "", con);
        cmd.ExecuteNonQuery();
        con.Close();
        refreshdata();
    

【问题讨论】:

能否分享完整的错误信息 请参加旅行***.com/tour,以便我们帮助您:-) 【参考方案1】:

首先,您没有对任何变量分配进行空值检查。

我的猜测是您尝试投射的单元格之一是空的。

【讨论】:

以上是关于我的gridview中的“对象引用未设置为对象的实例”的主要内容,如果未能解决你的问题,请参考以下文章

如何更改 YII 中的 GridView 列大小?

如何根据其上的 Sql 结果隐藏 gridview 中的列?

如何摆脱gridview WPF中的空表?

GridView在android中的可扩展列表中

如何删除GridView内LinearLayout中的空白?

GridView 未显示 DataTable 中的所有列