C#代码在按钮点击时更改asp.net中的密码 [英] C# Code to change password in asp.net on button click

查看:118
本文介绍了C#代码在按钮点击时更改asp.net中的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的.aspx代码。我有三个文本框和一个提交按钮

1 - 当前密码

2 - 新密码

3 - 确认密码



 <   body  < span class =code-keyword>>  
< 表格 id = form1 runat = 服务器 >
< div >

< br / >
< table class = style1 >
< tr >
< td = style2 > ;
当前密码< / td >
< td >
< asp:TextBox ID = TextBox1 runat = server > < / asp:TextBox >
< asp:HyperLink ID = HyperLink1 runat = server

< span class =code-attribute> NavigateUrl = 〜/ ForgotPassUser.aspx > 忘记密码?< / asp:HyperLink >
< / td >
< / tr >
< tr >
< td class = style2 >
新密码< / td >
< td >
< asp:TextBox ID = < span class =code-keyword> TextBox2 runat = server > < / asp:TextBox >
< / td >
< / tr >
< tr >
< td class = style2 >
确认新密码< / td >
< td >
< asp:TextBox ID = TextBox3 runat = 服务器 > < span class =code-keyword>< / asp:TextBox >
< / td >
< / tr >
< tr >
< td class = style2 >
< / td >
< td >
< asp:按钮 ID = Button1 runat = server 文字 = 保存设置 / >
< / td >
< / tr >
< / table >

< / div >
< / form >
< / body >

解决方案



请写下波纹管功能Button1下的ChangePassword()点击



这将适用于更改密码。如果有任何错误,请告诉我。



 私人  void  ChangePassword()
{
尝试
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter( select *来自tbllogin wherere username =' + txtUsername.Text + ',con );
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
string username = ds.Tables [ 0 ]。行[ 0 ] [ 1 ]。ToString();
string OLdpassword = ds.Tables [ 0 ]。行[ 0 ] [ 2 ]。ToString();
if (OLdpassword == txtOLDPassword.Text)
{
if (txtNewPassword.Text == txtConfirmPassword.Text)
{
con.Open();
SqlCommand cmd = new SqlCommand( update tbllogin set password =' + txtNewPassword.Text + 'where username =' + txtUsername.Text + ');
int querystatus = cmd.ExecuteNonQuery();
if (querystatus > 0
{
lblError.ForeColor = System.Drawing.Color.Green;
lblError.Text = 密码已成功更新;
}
else
{
lblError.ForeColor = System.Drawing.Color.Red;
lblError.Text = 执行错误;
}
}
else
{
lblError.ForeColor = System.Drawing.Color.Red;
lblError.Text = 密码未命中匹配;
}
}
else
{
lblError.ForeColor = System.Drawing.Color.Red;
lblError.Text = 旧密码不正确;
}
}
catch {}
最后 {con.Close(); }
}


Hai



如果你想更改密码作为你的设计,只需获取3个文本框值,首先使用db检查当前密码,然后比较新传递和确认传递,然后在用户表中更新。



如果你想要重置密码,你需要获取新的密码和用户的电子邮件地址,检查电子邮件地址是否正确与db,并在db中更新新密码,并通过邮件发送新的密码,bca你有用户的电子邮件地址。



以上2种方式你可以使用,一切都很简单,只需选择查询,比较值和更新值,在你的项目中你使用所有查询为什么你需要编码,只需使用你的exesting查询和chnage一点点。



试试这个链接

在c#asp.net中选择更新插入删除命令 [ ^ ]



问候

Aravind


这里我们回答成员在编程时遇到的具体问题。



我们无法处理整个源代码。你需要自己做。

如果你在编码时遇到任何困难,请随时回到这里并提出另一个问题来描述这个场景。



会员很乐意为您提供帮助。



Happy Coding。 :)

This is my .aspx code. I have three textboxes and one submit button
1-Current Password
2-New Password
3-Confirm password

<body>
    <form id="form1" runat="server">
    <div>
    
        <br />
        <table class="style1">
            <tr>
                <td class="style2">
                    Current Password</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:HyperLink ID="HyperLink1" runat="server" 

                        NavigateUrl="~/ForgotPassUser.aspx">Forgot Password ?</asp:HyperLink>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    New Password</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Confirm New Password</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                     </td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Save Settings" />
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body> 

解决方案

Hi,
Please write the bellow function "ChangePassword()" under Button1 click

This will work for the changing password.If any error comes please let me know.

private void ChangePassword()
    {
        try
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from tbllogin wherere username='" + txtUsername.Text + "'", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            string username=ds.Tables[0].Rows[0][1].ToString();
            string OLdpassword=ds.Tables[0].Rows[0][2].ToString();
            if (OLdpassword == txtOLDPassword.Text)
            {
                if (txtNewPassword.Text == txtConfirmPassword.Text)
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("update tbllogin set password='" + txtNewPassword.Text + "' where username='" + txtUsername.Text + "'");
                    int querystatus = cmd.ExecuteNonQuery();
                    if (querystatus > 0)
                    {
                        lblError.ForeColor = System.Drawing.Color.Green;
                        lblError.Text = "Password updated successfully";
                    }
                    else
                    {
                        lblError.ForeColor = System.Drawing.Color.Red;
                        lblError.Text = "Execution error";
                    }
                }
                else
                {
                    lblError.ForeColor = System.Drawing.Color.Red;
                    lblError.Text = "Password miss match";
                }
            }
            else
            {
                lblError.ForeColor = System.Drawing.Color.Red;
                lblError.Text = "Old password is incorrect";
            }
        }
        catch { }
        finally { con.Close(); }
    }


Hai

If u want to change password as ur design,just get 3 text box values,first check with current password with db and then compare new pass and confirm pass and then update in user table.

If u want to reset password,u need to get new password and user email address,check email address is correct or not with db,and update new password in db and also send new pass word via mail,bca u have user email address.

The above 2 way u can use,all are easy ,just select query ,compare values and update values,in ur project u use all queries why u need codings,just use ur exesting query and chnage little bit.

try this link
select update insert delete commands in c# asp.net[^]

Regards
Aravind


Here we answer specific questions which members face during programming.

We can't work on the whole source code. You need to do that yourself.
If you face any difficulty while coding, feel free to come back here and ask another question with specific issue describing the scenario.

Members will be happy to help you then.

Happy Coding. :)


这篇关于C#代码在按钮点击时更改asp.net中的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆