获取错误对象引用未设置为对象实例 [英] getting an error object referance not set to instance of an object

查看:80
本文介绍了获取错误对象引用未设置为对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么在gridview中删除时出现以下erorr错误.请找出问题所在.


i don''t know why i am getting error the following erorr while deleting in gridview. pls figure out whats the problem.


object refereance not set to instance of an object 



这是代码



here is the code

protected void _rowdeleting(object sender, GridViewDeleteEventArgs e)
   {
       //String Username = GridView1.DataKeys[e.RowIndex].Value.ToString();

       GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
       Label user = (Label)row.FindControl("UserName");
       string Username = user.Text;


       //string Username = System.Convert.ToString(User);
      // string Username = System.Convert.ToString(Username);

       // instantiate BAL
       Businesslayer pBAL = new Businesslayer();
       try
       {
           pBAL.Delete(Username);

           lblMessage.Text = "Record Deleted Successfully.";
       }
       catch (Exception ee)
       {
           lblMessage.Text = ee.Message.ToString();
       }
       finally
       {
           pBAL = null;
       }

      // GridView1.EditIndex = -1;
       // Refresh the list
       BindGrid();
   }

推荐答案

这取决于您在哪里得到错误:
It depends where you are getting the error:
string Username = user.Text;


表示找不到您的控件用户名.
否则,假设您的业务层是从List< T>派生的. (或类似),或支持IEnumerable-检查您的构造函数.如果应该填充列表,则似乎没有添加用户名条目.

假定您有一个try-catch块,我认为该错误出在您的标签中,这暗示该标签.


Would mean that your control UserName has not been found.
Otherwise, assuming that your Businesslayer is derived from List<T> (or similar), or supports IEnumerable - check your constructor. If it is supposed to populate the list then it doesn''t appear to be adding the Username entry.

Given you have a try-catch block, I assume the error is in your label, which would imply the latter.


如何填充gridview.
在该方法中,是否有任何会话或查询字符串值正在使用,请再次检查此内容.

然后检查一下,名称findcontrol("username")与网格视图项目模板标签id相同.是否存在任何拼写错误.
如果相同,则检查第一点.
希望它有用.
如果没有回复我,

来自
vikranth reddy.
How you are filling the gridview.
in that method is there any session or query string values are using once check this thing also.

and check this thing one, the name findcontrol("username") is same as grid view item template label id.is there any spelling mistakes.
if it is same then check first point.
hope it is useful.
if not reply me,

from
vikranth reddy.


这篇关于获取错误对象引用未设置为对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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