Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a registration page with field email and password stored in database.Now i want to log in using email and password field of database.
i applied query for that is::
i have enterd with email-name@gmail.com
and password-name
-----------------------------------------------------------------------------
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Registration where Email='" + loginemail + "' and password='" + loginpassword + "'", con);
SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read())
{
Response.Write("Login Successful");
Response.Redirect("Home.aspx");

}
else
{
Response.Write("Wrong user name and password");
}
------------------------------------------------------------------------------
after running this code always else part is working.while i m entering same email and password which i enterd into registration page.
plzz...tell me wt is problem in this..or tell wright query for this.
Posted
Comments
Sibasisjena 3-Jan-14 2:40am    
First of all reader is not required.
♥…ЯҠ…♥ 3-Jan-14 2:43am    
You sure you have data in the table?
Member 10279752 3-Jan-14 3:13am    
yes..data present is in table..
♥…ЯҠ…♥ 3-Jan-14 5:11am    
Totally weird... How it could be?

Since you are doing web development here, you may take a look at MembershipProvider[^].

As far a current code is concerned, check if you have data in the table. Also, here some other points you might want to consider:

1. Use parameterized queries. Your current code is prone to SQL injection.
2. Do you really need to return all the columns from database? You are not even using it.
 
Share this answer
 
First thing first, the way you code your sql queries is inviting SQL Injection. You must always parameterize your sql queries. The reason is here: give-me-parameterized-sql-or-give-me-death[^]. How to do it, check this out: sqlparameter[^]
As to your question, you may want to check the values of 'loginemail' and 'loginpassword' against those in the 'Registration' datatable. You can do this by using the Debug class, read this: Debug and Trace[^]. However, there could be other bugs exist elsewhere. You should be able to find them through debugging.
 
Share this answer
 
v3
Hi,

Why ExecuteReader? Is there any specific reason to do that? It will be helpful if you have multiple select statement.
You could use ExecuteScalar, which returns single value from DB, which suits your requirement, you just want to valid whether record exists or not then
Use the below code
C#
using (SqlConnection con = new SqlConnection(str))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("select COUNT(*) from Registration where Email='" + loginemail + "' and password='" + loginpassword + "'", con);

                     int isUserExist= Convert.ToInt32(cmd.ExecuteScalar());
                        if (isUserExist > 0)
                        {
                            Response.Write("Login Successful");
                            Response.Redirect("Home.aspx");
                        }
                        else
                        {
                            Response.Write("Wrong user name and password");
                        }
                }


Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
From your query it is clear that, there is no row in your reader

If you want to use reader, then use the following code .
C#
using (SqlConnection con = new SqlConnection(str))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("select * from Registration where Email='" + loginemail + "' and password='" + loginpassword + "'", con);

                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        if (dr.HasRows && dr.Read())
                        {
                            Response.Write("Login Successful");
                            Response.Redirect("Home.aspx");

                        }
                        else
                        {
                            Response.Write("Wrong user name and password");
                        }
                    }
                }


Check your reader has row or not
Check whether you have the data in table or not
 
Share this answer
 
v2
Comments
Member 10279752 3-Jan-14 3:39am    
@sibasis05 I tried this but giving same as before. (Else part is Working till nw)
Sibasisjena 3-Jan-14 4:34am    
There is no record in your database for the particular loginemail and loginpassword
Member 10279752 3-Jan-14 4:43am    
Boss i hv checked it. i have five entries in my db with loginemail and loginpassword emailid is-jainamit.it@gmail.com and password for that entry is-amit
bt wen i login using this it runs only else part..

plzz tell me another query/way for login. Thnxx
Sibasisjena 3-Jan-14 4:48am    
Can you show me your table structure in DB. I mean what are the columns you have. I will provide you a query. show me by taking screen shot of the data base
Member 10279752 3-Jan-14 4:53am    
These are coloums in database table

[dbo].[Registration](
[Firstname] [nvarchar](max) NOT NULL,
[lastname] [nvarchar](max) NOT NULL,
[phno] [nvarchar](max) NOT NULL,
[email] [nvarchar](max) NOT NULL,
[password] [nvarchar](max) NOT NULL,
[address] [nvarchar](max) NOT NULL,
[city] [nvarchar](max) NOT NULL,
[state] [nvarchar](max) NOT NULL,
[pincode] [nvarchar](max) NOT NULL,
[country] [nvarchar](max) NOT NULL,
[prefferddlvdt] [nvarchar](max) NOT NULL,
[deliveryinstruction] [nvarchar](max) NOT 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