Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=ABHINAV-PC\ABHI;Initial Catalog=Onlineexam;Integrated Security=True");
        con.Open();
        string cmd = "select * from Login where Institute='"+DropDownList1.Text+"'And Username='"+TextBox2.Text+"' and Password='"+TextBox3.Text+"'";
        SqlDataAdapter da = new SqlDataAdapter(cmd, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if ((ds.Tables[0].Rows.Count > 0))
        {
            Response.Redirect("Exam.aspx");
        }
        else {
            lblloginError.Text = "Username or password must be wrong"; 
        }
        
        
    }

What i'm doing wrong in this?
its not redirecting to the exam page...it is directly showing the error text...please help!
Posted
Updated 12-Jul-14 18:59pm
v2
Comments
What is that error?

C#
 con.Open();
                SqlCommand cmd = new SqlCommand("select * from Login where Institute='"+DropDownList1.Text+"'And Username='"+TextBox2.Text+"' and Password='"+TextBox3.Text+"'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        Response.Redirect("Exam.aspx");


                    }
                }
 else {
            lblloginError.Text = "Username or password must be wrong"; 
        }
con.Close();


try like this it will work ;)
 
Share this answer
 
Comments
Abhinav Chaudhary 3-Aug-14 8:09am    
still its not working...anyways Thanks for help!
u mani 4-Aug-14 0:42am    
what error ur getting ?
can't tell direct answer to your question with these information. you can find the solution by your self.
put break point just after below line and run the program
C#
string cmd = "select * from Login where Institute='"+DropDownList1.Text+"'And Username='"+TextBox2.Text+"' and Password='"+TextBox3.Text+"'";

check the value of cmd you get when you debug. do you have values you expect as institute, user name and password? if not you need to find why you not getting expected value.
if all these values are correct then you can copy the sql statement and run this directly on sql server management studio and confirm whether you have records for generated sql or not.
hope this helps you to find where is the issue and then you will able solve by user self or update the question with more specific details.

below are few side notes to improve your code:
1. use sql parameters[^]
2. dispose the connection with using block
 
Share this answer
 
v2
Comments
Abhinav Chaudhary 13-Jul-14 1:17am    
if ((ds.Tables.Count > 0))
i've done this to my code...but now it not checking and directly redirecting to the page...i think my code is not proper..
DamithSL 13-Jul-14 1:19am    
have you debug and check? you may have table but no records in it.
Abhinav Chaudhary 13-Jul-14 1:22am    
Yes...
DamithSL 13-Jul-14 1:24am    
i have told you to test few other things like parameter values and have you test final sql statement on sql server management studio?
Abhinav Chaudhary 13-Jul-14 3:04am    
yes sir...it is proper sql statement

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