Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
2.71/5 (3 votes)
See more:
Hello. I have a login page and it has two buttons. The Submit button takes the username and password and puts it in a security table. The login button takes the stored data from the security
table and lets the user login. I also have two tables that has the data record for all the users. How can I check to see if the user exists within those two tables in a single string in asp.net?
using System;
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(System.Configuration.ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
            con.Open();

            string cmdStr = "Select count(*) from TableSecurity where EmailAddress='" + TextBoxEA.Text + "'";
            string cmdStr2 = "Select count(*) from TableCEO where EmailAddress ='" + TextBoxEA.Text + "'";
            string cmdStr3 = "Select count(*) from TableIALO where EmailAddress ='" + TextBoxEA.Text + "'";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            SqlCommand cmd = new SqlCommand("select * from TableSecurity", con);
            SqlCommand cmd2 = new SqlCommand("select * from TableCEO", con);
            SqlCommand cmd3 = new SqlCommand("select * from TableIALO", con);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 1)

                Response.Write("User Name Already Exist!!!<br /> Please Choose Another User Name.");
        }
    
    }

    
    protected void Submit_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
        con.Open();

        string chkUser = "select count(*) from TableCEO where EmailAddress'" + TextBoxEA.Text +"";
            chkUser = "select count(*) from TableIALO where EmailAddress'" + TextBoxPW.Text + "";
            SqlCommand chkUsercmd = new SqlCommand(chkUser, con);
            

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

        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            Response.Write("Something Really Bad Has Happened....Please Try Again.");
        }
        finally
        {
        }
        }

    
    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
        con.Open();

        string cmdStr = "select count(*) from TableCEO where EmailAddress= '" + TextBoxEA.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 = '" + TextBoxPW.Text + "'";
            SqlCommand pass = new SqlCommand(cmdStr2, con);
            string password = pass.ExecuteScalar().ToString();
            con.Close();

            if(password == TextBoxPW.Text)
            {
                Session["New"] = TextBoxEA.Text;
                Response.Redirect("Secure.aspx");
            }
            else
            {
                Label1.Visible = true;
                Label1.Text = "Invalid Password...!!";
            }
        }
            else
            {
                Label1.Visible = true;
                Label1.Text = "Invalid EmailAddress...!!";
        }
    }
}
Posted
Updated 30-Apr-13 9:42am
v3
Comments
Richard C Bishop 30-Apr-13 15:22pm    
You can use an inner join when querying the tables. That will allow you check both tables I believe.
Computer Wiz99 30-Apr-13 15:33pm    
Can you show me what the inner join code looks like?
Richard C Bishop 30-Apr-13 15:36pm    
Here is a generic select statement with an inner join. You will have to modify to fit your needs:

SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Computer Wiz99 30-Apr-13 15:38pm    
Ok. Thanks. Can I put this in ASP.Net using C#? Where will I put this statement?
Computer Wiz99 30-Apr-13 15:38pm    
Here is the code I updated.

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