C# Login error

If the login is correct, then it the code works fine. If the login is not correct, then I get this error message.

"System.NullReferenceException: Object reference not set to an instance of an object. at ASP.login_aspx.VerifyUser(String email, String password) in c:\Documents and Settings\JJ.HOME\My Documents\C #\GridviewGuy\Login.aspx:line 52 at ASP.login_aspx.btnLogin_Click(Object sender, EventArgs e) in c:\Documents and Settings\JJ.HOME\My Documents\C #\GridviewGuy\Login.aspx:line 27"

private string connectionString
    {
        get
        {
            return @"Server=workserver;Database=webdesignerguys;Trusted_Connection=true";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Write(VerifyUser(txtEmail.Text, txtPassword.Text));
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }

    public string VerifyUser(string email, string password)
    {
        SqlConnection connection = new SqlConnection(connectionString);
        String sql = "SELECT UserID FROM USERS WHERE email ='" + email + "'and password ='" + password + "'";
        SqlCommand command = new SqlCommand(sql, connection);
        int recordcount = 0;
        string message = string.Empty;
        //Response.Write(sql);
        try
        {
            connection.Open();
            recordcount = (int)command.ExecuteScalar();
            connection.Close();
        }

        catch (Exception ex)
        {
            throw ex;
        }

        finally
        {
            if (connection != null)
            {
                connection.Close();
            }
        }


        if (recordcount > 0)
        {
            message = "Correct login";
        }
        else
        {
            message = "Incorrect login";
        }
       
        return message;
    }
LVL 8
JRockFLAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

daniel_ballaCommented:
because the ExecuteScalar method returns the first column of the first row in the result set, or a null reference if the result set is empty. It doesn't return 0. so you either change the sql query, or the method used (use an sql reader instead) or if you are doing this in VS 2005 you can use int? instead of int.
0
JRockFLAuthor Commented:
Ok that makes sense, how can I do it without using the sql reader instead? Yes, I am using VS2005 (Express)

I was trying to cast this as int, is that not correct?
recordcount = (int)command.ExecuteScalar();



0
daniel_ballaCommented:
JRockFL,
no, that doesn't do, but instead you can use int? data type (nullable)
so you do
int? recordcount;
.........
recordcount = (int?)command.ExecuteScalar();
........
if (recordcount != null)
        {
            message = "Correct login";
        }
        else
        {
            message = "Incorrect login";
        }
0
JRockFLAuthor Commented:
Do you mean Int32 ?

 recordcount = (Int32)command.ExecuteScalar();
0
daniel_ballaCommented:
no, I mean int?
as in nullable integer.
You can read more about nullable data types in .NET 2.0 here: http://msdn2.microsoft.com/en-us/library/2cf62fcy.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.