Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Errors are - 1 ) Use the "New" keword to create the object
2) Object Reference not set to an instance

Error is giving in this line -

C#
rs = ds.Tables["registration"].Select("email='" + sa + "'");


My code in cs is


protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection CON = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["tuitionConnectionString"].ConnectionString);
        CON.Open();
        ds = new DataSet();
        string sql = "select *  from registration";
        da = new SqlDataAdapter(sql, CON);
        da1 = new SqlDataAdapter(sql, CON);
        string sa = txtEmail.Text;
        string pw = txtPass.Text;
        int r = 0, r1 = 0;
        DataRow[] rs;
        DataRow[] rs1;
        rs = ds.Tables["registration"].Select("email='" + sa + "'");
        r = rs.Length;
        
        rs1 = ds.Tables["registration"].Select("pass='" + pw + "'");
        r1 = rs1.Length;
        
        if (r!=0 && r1!=0)
        {
            txtEmail.Text = r.ToString();
            txtPass.Text = r1.ToString();
          
        }
        else
        {
            System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
            lbl1.ForeColor = System.Drawing.Color.Yellow;
            lbl1.BackColor = System.Drawing.Color.Blue;
            lbl1.Text = "Please Enter a Valid User ID and Password";
            ph1.Controls.Add(lbl1);
        }
        CON.Close();
    }


[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Posted
Updated 18-Dec-11 21:14pm
v2

Put a break point at that line and when program execution reached to that point check whether ds.Tables["registration"] or sa not to be null.
If one of them where null do proper actions .

Hope it helps.
 
Share this answer
 
You need to look at how to populate a Dataset[^].

Use the fill method of the Data Adapter on the Dataset.
Something like
da.fill(ds,"registration");
 
Share this answer
 
Look at your code:

C#
ds = new DataSet();
string sql = "select *  from registration";
da = new SqlDataAdapter(sql, CON);
da1 = new SqlDataAdapter(sql, CON);
string sa = txtEmail.Text;
string pw = txtPass.Text;
int r = 0, r1 = 0;
DataRow[] rs;
DataRow[] rs1;
rs = ds.Tables["registration"].Select("email='" + sa + "'");
Taking out all the lines which do not refer to (and thus affect) ds in any way:
ds = new DataSet();
rs = ds.Tables["registration"].Select("email='" + sa + "'");
Which means that you are ctreateing a new, empty DataSet, and then trying to use a table from it. It is empty, so no tables, so you get the "object Reference..." error.
I suspect that you need an SqlDataAdapter.Fill(ds) from either da or da1 before you try to use the DataSet.
I don't know why you have two identical SqlDataAdapters, though...
 
Share this answer
 
Comments
Janardan Pandey 19-Dec-11 3:37am    
Thanks i have solved my problem by ur suggestion.

Thanks

Janardan Pandey
OriginalGriff 19-Dec-11 3:51am    
You're welcome.
It looks like ds.Tables["registration"] returns null. You could have misspelled this hard-coded string index or something like that. Not a big deal. Always use the Debugger to avoid such problems. And such questions, by the way.

"Use 'new'…" is a bogus suggestion. Sometime you need to use "new", sometimes not.

—SA
 
Share this answer
 
v3
Comments
Amir Mahfoozi 19-Dec-11 3:26am    
+5
Sergey Alexandrovich Kryukov 19-Dec-11 4:08am    
Thank you, Amir.
--SA
koolprasad2003 19-Dec-11 3:35am    
Yes, simple ds.Tables["registration"] is NULL. 5.
Sergey Alexandrovich Kryukov 19-Dec-11 4:08am    
Thank you, Prasad.
--SA

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