Click here to Skip to main content
15,311,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
getting object reference error

What I have tried:

string k = Session["uid"].ToString();
        u_id.Text = k;

        if (!IsPostBack)
            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\helping.mdf;Integrated Security=True;User Instance=True");
            String query = "select * from adoptionreg";
            SqlCommand com = new SqlCommand(query, con);
            SqlDataReader sdr;
            sdr = com.ExecuteReader();
            int i = 1120;
            while (sdr.Read())
            a_id.Text = "A" + i;

Updated 13-Aug-18 19:52pm
F-ES Sitecore 14-Aug-18 5:31am
There will be an issue with one of the hundred lines of code you posted. Seeing as we don't know what line the error occurred on it's hard to give any concrete advice.

1 solution

after: con.Open();
SqlDataReader sdr = NEW SqlDataReader();

might help.

at the bottom a_id might have to be u_id (not sure, can't see all the code)
OriginalGriff 14-Aug-18 1:53am
Nope, the ExecuteReader method returns a new reader.
It's probably the Session doesn't contain a "uid" value.
AnotherKen 14-Aug-18 12:22pm
Ok, well, the error message means you tried to use an uninstantiated object. Either a property or method. This throws an error because if the object is not instantiated, it does not exist on the heap which is where it is looked for when you try to access it, that causes an exception to be thrown. Any object you use, even one in a library you did not write, has to be insantiated with the NEW keyword so that you can use it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900