Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am using web application.

on my login button i have written this code
protected void btnlogin_Click(object sender, EventArgs e)
    {
        //sql connection
        SqlConnection cnn = new SqlConnection();
        cnn.ConnectionString = "connection string";
        cnn.Open();
        SqlCommand cmd = new SqlCommand("select mobileno,password from register where mobileno='"+txtmoblie.Text +"'and password='"+txtpassword.Text+"'",cnn);
        cmd.Connection = cnn;
        SqlDataReader dr = cmd.ExecuteReader();
      
        if (txtmoblie.Text != "mobileno" && txtpassword.Text != "password")
        {
            Response.Write(@"<script language='javascript'>alert('Invalid Username and Password')</script>");
        }
        else
        {
            Response.Redirect("signup.aspx");
        }
        cnn.Close();
}


It is giving me invalid username and password
please let me know where i am wrong


Thanks in advance.
Posted
Comments
Orcun Iyigun 10-Dec-11 4:33am    
Have you debugged what values are you getting in the "if" statement? Do your "Select" statement actually returns a value?

relpace the following and try:
C#
if (txtmoblie.Text != "mobileno" && txtpassword.Text != "password")

with
C#
if (txtmoblie.Text != dr["mobileno"] && txtpassword.Text != dr["password"])
 
Share this answer
 
Would you like a list?
Where should I start....
0) You do realize that you won't get an SQL connection working with the string "connection string"? You need to tell it which SQL server instance, and database to use.
1) Why are you retrieving the password and mobile number from the database, based on the mobile number and the password? All you would be doing would be pulling back the information you already have. Either retrieve a more useful column, or retrieve a count.
2) Why are you leaving yourself wide open to SQL Injection attacks which can accidentally or deliberately destroy your database? Use Parametrized queries instead.
3) Why are you storing passwords in plain text? All you are doing is leaving everybodies user information wide open to everyone who wants to look at it. Hash it instead and store the hash. There is info on this here: Password Storage: How to do it.[^]
4) Why aren't you actually using any of the results from your database query? All you test does is say "log in anyone who has the mobile number 'mobileno' and the password 'password'
5) You do realize that even if you fixed these things, all that would happen is the user would get annoyed? Because he would type in his details, you would check them, then throw them away when you re-direct to the signup.aspx page?


Does that sound harsh? Yes, it probably does.

Try to do one thing at a time. Establish your connection, and make sure it works. Then move on to retrieving infor from teh DB, and so forth.

Look at your course notes, and try to work out what you are supposed to do here...:laugh:
 
Share this answer
 
Comments
Orcun Iyigun 10-Dec-11 4:39am    
5'ed so true. I was thinking the similar stuff to say but i didnt. glad that you pointed it out.
OriginalGriff 10-Dec-11 4:51am    
Yes, it seems cruel to say it all, but I guess it is crueller to say "you are doing well, just fix this" and then have it still not work.
aayu 11-Dec-11 23:47pm    
Dear OG i know that i have just remove that line coz i don't want to show so i kept connection string
aayu 11-Dec-11 23:50pm    
Second thing i know what you trying to say but its just i want to check that when a person is login it shuold go to other page that y i have kept signup coz other pages which i have create are all master page which i don't want to touch
Hi,

Your problem is connection is not open with database.
so that follow below code for data base connection, fill four connection parameter as per your sql database.

C#
string ConnectionString ="Data Source=;Initial Catalog=;User ID=;Password=";
        SqlConnection cnn = new SqlConnection(ConnectionString);
        cnn.Open();
 
Share this answer
 
Try this perhaps.

C#
if (dr[0] != "mobileno" && dr[1] != "password")
        {
            Response.Write(@"<script language="'javascript'">alert('Invalid Username and Password')</script>");
        }


If that is really your point :)

Regards,
Eduard
 
Share this answer
 
protected void btnlogin_Click(object sender, EventArgs e)
    {
        SqlConnection cnn = new SqlConnection("__your connection string__");
        cnn.Open();
        SqlCommand cmd = new SqlCommand("select mobileno,password from register where mobileno='"+txtmoblie.Text +"'and password='"+txtpassword.Text+"'",cnn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.read())//this is what you need
        {
        Response.Redirect("signup.aspx");
        }
        else
        {
        Response.Write(@"<script language="'javascript'">alert('Invalid Username and Password')</script>");
        }
        cnn.Close();
}
 
Share this answer
 
Is it connection string is successful?

C#
cnn.ConnectionString = "connection string";
        cnn.Open();
 
Share this answer
 
v2

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