错误-对象引用未设置为对象的实例. [英] Error - Object reference not set to an instance of an object.

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

问题描述

为什么给出此错误对象引用未设置为对象的实例.

在代码行中给出-
rs=ds.Tables["registration"].Select("umail=''" + t1.Text.Trim() + "''");

我的代码如下.

Why this error is given Object reference not set to an instance of an object.

In the code line is given -
rs=ds.Tables["registration"].Select("umail=''" + t1.Text.Trim() + "''");

My Code is given below.

string u=t1.Text;
string m;
int r=0;
DataRow []rs;
rs=ds.Tables["registration"].Select("umail='" + t1.Text.Trim() + "'");
r=rs.Length;
if(t1.Text=="")
{System.Web.UI.WebControls.Label lbl1=new System.Web.UI.WebControls.Label();
lbl1.ForeColor=System.Drawing.Color.Blue;
lbl1.BackColor=System.Drawing.Color.Yellow;
lbl1.Text="Please Fill the form for entry";
ph1.Controls.Add(lbl1);
}else if(r>0)
{System.Web.UI.WebControls.Label lbl1=new System.Web.UI.WebControls.Label();
lbl1.ForeColor=System.Drawing.Color.Blue;
lbl1.BackColor=System.Drawing.Color.Yellow;
lbl1.Text="Record Already EXIST";
ph1.Controls.Add(lbl1);
}
else
{

com=con.CreateCommand();
com.CommandText="Insert into registration (uemail, ufname, ulname, udob, uloc, uphone, utown, ustate, uskill, urole,uindustry, utexp, urexp, ucv, upass) values('" + t1.Text.Trim() + "','" + t2.Text.Trim() + "','" + t3.Text.Trim() + "','" + dob.ToString() + "','" + ta1.Value + "','" + t4.Text.Trim() + "','" + d4.SelectedItem.Text + "','" + d5.SelectedItem.Text + "','" + t5.Text.Trim() + "','" + t6.Text.Trim() + "','" + d6.SelectedItem.Text + "','" + t7.Text.Trim() + "','" + t8.Text.Trim() + "','" + ta2.Value + "','" + p.ToString() + "')";
try
{com.ExecuteNonQuery();
System.Web.UI.WebControls.Label lbl1=new System.Web.UI.WebControls.Label();
lbl1.ForeColor=System.Drawing.Color.Blue;
lbl1.BackColor=System.Drawing.Color.Yellow;
lbl1.Text="Data Entred";
ph1.Controls.Add(lbl1);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
con.Close();

推荐答案

这种错误的一种可能是-dsds.Tables["registration"]的值可能是null .

调试代码并检查ds.Tables["registration"]的值.

您也可以在代码中添加以下条件.
One possibility for this error I see is - value of ds or ds.Tables["registration"] could be null.

Debug your code and check value of ds.Tables["registration"].

Also you may add below condition in your code.
if (ds.Tables.Count > 0 && ds.Tables["registration"] != null)
{
 rs = ds.Tables["registration"].Select("umail='" +    t1.Text.Trim() + "'");
 r = rs.Length;
}


在调试器下运行程序,看看哪一行是null对象.
Run your program under a debugger and see which part of line is a null object.


此当您尝试访问null的对象(引用类型)时,将给出异常(此对象不引用任何对象的实例).

This exception is given when you try to access an object (a reference type) that is null (This object doesn''t reference to any object''s instance).

ds的值可能是null,或者没有称为注册"的表.

Probably, the value of ds is null or, there is no table that is called "registration".

您可以将第3-6行更改为:

You can change lines 3 - 6 to:

DataTable dt = ds != null ? ds.Tables["registration"] : null;
DataRow[] rs = dt != null ? dt.Select("umail='" + t1.Text.Trim() + "'") : null;
int r = rs != null ? rs.Length : 0;


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

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