Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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();
Posted
Updated 30-Oct-11 21:41pm
v2

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.
C#
if (ds.Tables.Count > 0 && ds.Tables["registration"] != null)
{
 rs = ds.Tables["registration"].Select("umail='" +    t1.Text.Trim() + "'");
 r = rs.Length;
}
 
Share this answer
 
v2
Comments
Mehdi Gholam 31-Oct-11 3:47am    
5'ed
RaisKazi 31-Oct-11 3:49am    
Thank you Mehdi. :)
Run your program under a debugger and see which part of line is a null object.
 
Share this answer
 

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).


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


You can change lines 3 - 6 to:


C#
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;
 
Share this answer
 
v2

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