Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
below is my code.m trying to verify username/password with database.but,its not working,when i debug it,next if to dr declaration(with bold latters) says that INVALID ATTEMPT TO READ WHEN NO DATA IS PRESENT.why this is so?any idea..

C#
private void button1_Click(object sender, EventArgs e)
        {
            
            if (string.IsNullOrEmpty(this.textBox1.Text) | string.IsNullOrEmpty(this.textBox2.Text))
            {
                MessageBox.Show("Please Enter Username or Password");
                return;
            }
            SqlConnection conn=new SqlConnection("Data Source=PRANAV\\SQLEXPRESS;Initial Catalog=Try;Integrated Security=True");
            
            conn.Open();
            
            SqlCommand cmd = new SqlCommand("SELECT * FROM idpswd where id='"+textBox1.Text+"'and pswd='"+textBox2.Text+"'",conn);



            /*SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);*/
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();

            if(dr.Read())
            {
                SqlConnection con = new SqlConnection("Data Source=PRANAV\\SQLEXPRESS;Initial Catalog=Try;Integrated Security=True");

                con.Open();
                if (this.textBox1.Text == dr["id"].ToString() & this.textBox2.Text == dr["pswd"].ToString())
                {
                    MessageBox.Show("Login Successfull!");

                }
                else
                {
                    MessageBox.Show("Invalid username or password");
                    return;
                }

            }
            
        }
Posted
Updated 30-Oct-12 18:12pm
v2

1 solution

First of all is there any data in the database provided your username & password matches, if yes it should come. I guess you providing the right connection string so its able to open the connection. while debugging check the connection after open, copy the cmd text query & execute in the SQL server. if the data comes then it should come in your application too. it might be you providing wrong data or there is no data in the database which matches with your username & password. try dr[0] & dr[1] instead of dr["id"] & dr["pswd"].

Check this link for more information.[^]

try this solution. Hope it helps.

C#
private void button1_Click(object sender, EventArgs e)
        {
            
            if (string.IsNullOrEmpty(this.textBox1.Text) | string.IsNullOrEmpty(this.textBox2.Text))
            {
                MessageBox.Show("Please Enter Username or Password");
                return;
            }
            SqlConnection conn=new SqlConnection("Data Source=PRANAV\\SQLEXPRESS;Initial Catalog=Try;Integrated Security=True");
            
            conn.Open();
            
            SqlCommand cmd = new SqlCommand("SELECT * FROM idpswd where id='"+textBox1.Text+"'and pswd='"+textBox2.Text+"'",conn);
 

 
            /*SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);*/
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
 
            if(dr.Read())
            {
                if (this.textBox1.Text == dr["id"].ToString() & this.textBox2.Text == dr["pswd"].ToString())
                {
                    MessageBox.Show("Login Successfull!");
 
                }
                else
                {
                    MessageBox.Show("Invalid username or password");
                    return;
                }
 
            }
            
        }
 
Share this answer
 
Comments
Thanks7872 31-Oct-12 0:37am    
thanks for your reply.issue is resolved,the problem was too too silly..!!!!i have stored the username in database which has space at first character by mistake,and m trying login without space,there was nothing wrong with that code.it works fine now,thanks for your precious consideration.
Sushil Mate 31-Oct-12 0:49am    
No worries, it happens.

try my solution, you are opening one connection twice. which is not needed.

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