我无法使用c#在asp.net中更改密码 [英] I am not able to change password in asp.net using c#
本文介绍了我无法使用c#在asp.net中更改密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是登录页面的按钮点击代码,我在会话中有商店公司ID
This is on button click code of login page and i have store company id in session
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>");
}
}
这是按钮点击更改密码的代码。我有三个文本框
1 - 当前密码
2 - 新密码
3-确认新密码
我得到的错误是这个lable-lblError.Text =请输入正确的当前密码;
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";
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";
}
}
推荐答案
尝试使用datatable而不是datareader ..可能吧将帮助......!
Try to use datatable instead of datareader ..may b it will help...!
尝试使用数据集而不是datareader.like
Hi,
Try to use dataset instead of datareader.like
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";
}
}
在这里,您可以获得所有公司记录,这就是您收到错误的原因。修改您的查询,如下所示。
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.
这篇关于我无法使用c#在asp.net中更改密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文