Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a Login problem in ASP.NET. I have two tables that has user information in them already. What I want my login to do is to check to see if the username exist within those tables. If they do then the user can make up a password and login. The username and password are now saved in the Security Table. How can I get it to check the user in both tables? And what did I do wrong within my code to get an error message: "Object reference not set to an instance of an object". It happens on this line of code: Line 51: string password = pass.ExecuteScalar().ToString();. Here is my whole code:

C#
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
            con.Open();
            string cmdStr = "select count(*) from TableCEO where EmailAddress='" + TextBox1.Text + "'";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            con.Close();
            if (temp == 1)
            {
                Response.Write("User Name Already Exist!!!");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
        con.Open();

        string insCmd = "Insert into TableSecurity (EmailAddress, Password, Level) values (@EmailAddress, @Password, @Level)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBox1.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBox2.Text);
        insertUser.Parameters.AddWithValue("@Level", TextBox1.Text);





        string cmdStr = "select count(*) from TableCEO where EmailAddress='" + TextBox1.Text + "'";
            SqlCommand Checkuser = new SqlCommand(cmdStr, con);
            int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());
            if (temp == 1)
            {
                string cmdStr2 = "Select Password from TableSecurity where Password='" + TextBox2.Text + "'";
                SqlCommand pass = new SqlCommand(cmdStr2, con);
                string password = pass.ExecuteScalar().ToString();
                con.Close();

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

                
            }
        }
    }


Please Help me!!!
Posted
Updated 29-Apr-13 6:23am
v2
Comments
[no name] 29-Apr-13 12:24pm    
You would get that error on that line because your SQL-injection-attack prone query is not returning anything.
Computer Wiz99 29-Apr-13 12:26pm    
Ok. So, How to correct that problem?
[no name] 29-Apr-13 12:28pm    
Simple, either construct a query that returns a value or check the value that is being returned to make sure that it is not null.
Computer Wiz99 29-Apr-13 12:33pm    
Can you give more of a code example?
[no name] 29-Apr-13 12:40pm    
Why? Do you not know how to check something for being null?

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