[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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;
    }
0
JRockFL
Asked:
JRockFL
  • 3
  • 2
1 Solution
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now