Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want user to login by any of the one, either username or Email id. I already have the code for login through username but i want it to be as either username or email address. what changes to make in below code ??


C#
protected void Button1_Click(object sender, EventArgs e)
       {
           SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");

           con.Open();
           SqlCommand cmd = new SqlCommand("select * from Users where Username=@username and Password=@password", con);
           cmd.Parameters.AddWithValue("@username", uname.Text);
           cmd.Parameters.AddWithValue("@password", pwd.Text);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           if (dt.Rows.Count > 0)
           {
               Session["UserID"] = dt.Rows[0]["UserID"].ToString();
               Session["FirstName"] = dt.Rows[0]["FirstName"].ToString();
               Session["Username"] = uname.Text;
               Response.Redirect("StudentDashboard.aspx");
           }
           else
           {
               ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
           }
       }
Posted

Modify your select query like this:


select * from Users where (Username=@username or Email=@email) and Password=@password
 
Share this answer
 
Comments
jayraj86 8-May-14 2:39am    
thanks
Jadam Hades 24-Jan-20 19:36pm    
Thanks
U can add EmailId column to your Users table and query data as 'select * from Users where (Username=@username or Email=@email) and Password=@password'
 
Share this answer
 

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