1

I am always getting "Object reference not set to an instance of an object". I dont know what i did wrong. I did try to execute the query: "select count(*) from Registration where Username='Myname' and it returned 1;

The code:

 protected void Button1_Click(object sender, EventArgs e)
    {

            DBConnect con = new DBConnect();
            string query = "select count(*) from Registration where Username='" + TextBox1.Text + "'";

            //Open connection
            if (con.OpenConnection() == true)
            {
                //Create Command
                MySqlCommand cmd = new MySqlCommand(query, con.Initialize());
                //Create a data reader and Execute the command

                int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());

                if (temp == 1)
                {
                    string query1 = "Select Password from Registration where Username='" + TextBox1.Text + "'";
                    MySqlCommand pass = new MySqlCommand(query1, con.Initialize());
                    string password = pass.ExecuteScalar().ToString();

                    if (password == TextBox2.Text)
                    {
                        Session["new"] = TextBox1.Text;
                        Response.Redirect("GreenhouseHomepage.aspx");
                    }
                    else
                    {
                        Label1.Text = "Invalid Password...!!";
                        Label1.Visible = true;
                    }
                }
                else
                {
                    Label1.Text = "Invalid Username...!!";
                    Label1.Visible = true;
                }
            }

            //close Connection
            con.CloseConnection();           
    }

The Error: enter image description here

user3346509
  • 57
  • 1
  • 7
  • 4
    This is slightly off topic but I am constantly amazed by the prevalence of SQL injection in code samples posted on SO. Please [educate yourself](http://www.troyhunt.com/2013/07/everything-you-wanted-to-know-about-sql.html). It also looks like you're storing passwords in plain text: `MySqlCommand pass = new MySqlCommand(query1, con.Initialize());` – DGibbs Apr 03 '14 at 14:56
  • I wouldn't use your `DBConnect`-class at all in ASP.NET. Otherwise you are in danger of running into several issues sooner or later due to open connections. Instead use the `using`-statement. I'm also afraid that your connection instance is `static` which would be a very bad idea in ASP.NET. – Tim Schmelter Apr 03 '14 at 14:59
  • Off the top of my head I would say that `ExecuteScalar` does not return a value, ie. it returns `null` and thus dereferencing `null` to call `.ToString()` throws that exception. – Lasse V. Karlsen Apr 03 '14 at 15:05
  • Have you tried this `MySqlCommand cmd = new MySqlCommand(query, con);`? – Mad Dog Tannen Apr 03 '14 at 15:06
  • Thanks all :) @Tim Schmelter i did what you told me and that's worked. I just used my connection directly in ASP.net instead of using it inside my .dll reference. – user3346509 Apr 03 '14 at 15:20
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dour High Arch Apr 03 '14 at 16:01
  • Not every question which contains a Nullreferenceeyception is a duplicate of "What is a NullReferenceException and how do I fix it?". – Tim Schmelter Apr 03 '14 at 16:11

1 Answers1

0

Give this a try.

//Open connection
            if (con.OpenConnection() == true)
            {
                // Since you know that the connection is open, you can just 
                // pass the entire conn object
                MySqlCommand cmd = new MySqlCommand(query, con);
                //Create a data reader and Execute the command

                int temp = Convert.ToInt32(cmd.ExecuteScalar());

The COUNT(*) will return 0 if no records are found so you will always get a value from your syntax.

Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55