Click here to Skip to main content
15,914,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hI,

can anyone know why dr.hasrows shows false even data present in DB??


C#
 protected void btnchange_Click(object sender, EventArgs e)
    {
        int changed;
        string loginemailid = txtloginemailid.Text.Trim();
        string oldpass = txtoldpassword.Text.Trim();
        string newpass = txtnewpassword.Text.Trim();
        
            SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["constring"].ToString());
            //SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
            cn.Open();
            SqlCommand cmd = new SqlCommand("update tbl_register set password='" + newpass  + "',conpassword='" + newpass + "' where loginemailid='" + loginemailid  + "' and password='"+ oldpass +"'", cn);
            SqlDataReader dr=cmd.ExecuteReader();
            if (dr.HasRows)
            {
                if (dr.Read())
                {
                    loginemailid = dr["loginemailid"].ToString();
                    oldpass = dr["password"].ToString();
                    newpass = dr["password"].ToString();
                }
                dr.Close();
    }
           
            else
            {
                lblmsg.Text = "Updation Failed,Ensure Your Login Information Is Correct";
            }
    }
}
Posted
Updated 12-Feb-12 22:55pm
v2

Because you are making an update command not selecting(ie reading) records,You should first select the records then update password if reader reads the records
 
Share this answer
 
v2
Comments
Herman<T>.Instance 13-Feb-12 5:06am    
that is indeed the reason!
You are using upadate command.
SqlDataReader work only with select command.
here you should use
C#
cmd.ExecuteNonQuery();

C#
SqlCommand cmd = new SqlCommand("update tbl_register set password='" + newpass  + "',conpassword='" + newpass + "' where loginemailid='" + loginemailid  + "' and password='"+ oldpass +"'", cn);

          cmd.ExecuteNonQuery();
lblmsg.Text = "Record Updated";
 
Share this answer
 
v3
Comments
RDBurmon 13-Feb-12 5:32am    
my +5
uspatel 13-Feb-12 5:33am    
Thanks Rahul.......
You do an update query - that does not return any rows. You need a select query to get the values.
Since you want to update and check, do:
C#
int n=cmd.ExecuteNonQuery();
if (n==1)
{
    //OK
}
else
{
    //failure
}
 
Share this answer
 
v2
SqlDataReader used in select command,if you use SqlDataReader in update command,the returned SqlDaraReader has No Data,so the returned SqlDataReader.HasRows is false.
 
Share this answer
 
Comments
RDBurmon 13-Feb-12 5:31am    
my +5

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