Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is on button click code of login page and i have store company id in session
C#
protected void clogin_Click(object sender, EventArgs e)
       {
           SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");

           con.Open();
           SqlCommand cmd = new SqlCommand("select * from Companies where UserName=@cusername and Password=@password", con);
           cmd.Parameters.AddWithValue("@cusername", cuname.Text);
           cmd.Parameters.AddWithValue("@password", cpwd.Text);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           if (dt.Rows.Count > 0)
           {
               Session["CompanyID"] = dt.Rows[0]["CompanyID"].ToString();
             //  Session["Username"] = cuname.Text;
               Response.Redirect("CompanySettings.aspx");
           }
           else
           {
               ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
           }

       }


This is on button click code of change password. i have three text boxes
1-Current Password
2-New Password
3- confirm new password

the error i got is this lable- lblError.Text = "Please enter correct Current password";
C#
protected void Button1_Click(object sender, EventArgs e)
        {

            ChangePassword();
        }

        private void ChangePassword()
        {
            con.Open();
            string str = "select * from Companies";
            SqlCommand com = new SqlCommand(str, con);
            SqlDataReader reader = com.ExecuteReader();

            while (reader.Read())
            {
                if (currentpassword.Text == reader["Password"].ToString())
                {
                    //   up = 1;

                    // con.Open();
                    str = "update Companies set Password='" + newpassword.Text + "'  where CompanyID=" + Session["CompanyID"].ToString() + "";
                    com = new SqlCommand(str, con);

                    com.ExecuteNonQuery();
                    con.Close();


                    lblError.Text = "Password changed Successfully";

                }
                else
                {


                    lblError.Text = "Please enter correct Current password";

                }
            }
Posted
Comments
ArunAmalraj 11-Mar-14 7:29am    
if (currentpassword.Text == reader["Password"].ToString())

I think it you should check != instead of using == .

if (currentpassword.Text != reader["Password"].ToString())
ArunAmalraj 11-Mar-14 7:29am    
I was wrong.
ZurdoDev 11-Mar-14 7:31am    
Just debug it and see what is happening.
ArunAmalraj 11-Mar-14 7:31am    
Try using Val(currentpassword.Text) instead of currentpassword.Text
jayraj86 11-Mar-14 7:36am    
The name 'Val' does not exist in current context

Try to use datatable instead of datareader ..may b it will help...!
 
Share this answer
 
Hi,
Try to use dataset instead of datareader.like
C#
    string str = "select * from Companies where CompanyID='" + Session["CompanyID"].ToString() + "'";
SqlCommand com = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
if(ds.Tables[0].Rows.Count>0)
{
    if(currentpassword.Text ==ds.Tables[0].Rows[0]["password"])
    {
        // con.Open();
            str = "update Companies set Password='" + newpassword.Text + "'  where CompanyID=" + Session["CompanyID"].ToString() + "";
            com = new SqlCommand(str, con);

            com.ExecuteNonQuery();
            con.Close();


            lblError.Text = "Password changed Successfully";
    }
}
 
Share this answer
 
v2
Comments
jayraj86 11-Mar-14 7:52am    
Same error !!
jayraj86 11-Mar-14 7:58am    
and warning on line if(currentpassword.Text ==ds.Tables[0].Rows[0]["password"])

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'string'
SanSkun 11-Mar-14 8:01am    
sorry please write
if(currentpassword.Text ==ds.Tables[0].Rows[0]["password"].ToString())
jayraj86 11-Mar-14 8:04am    
same error

Please enter correct Current password
SanSkun 11-Mar-14 8:07am    
Can u please check by giving breakpoint at if(currentpassword.Text ==ds.Tables[0].Rows[0]["password"].ToString()) and check the value of both the field.Because it will work fine.Your if block is not executing.Please check that value once and let me know the error for same.Or else check this working fine

http://www.codeproject.com/Answers/741439/Csharp-Code-to-change-password-in-asp-net-on-but#answer2
Here you getting all company records thats why you are getting error. Modify your query as shown below.
"select * from Companies where CompanyId="+Convert.ToInt32(Session[CompanyId]); 

This may help you.
 
Share this answer
 
Comments
jayraj86 11-Mar-14 9:24am    
not working

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