在C#中更新密码 [英] Updating a password in c#

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

问题描述

新:我删除了第二个打开的连接,但是现在我收到另一个错误,该错误是已经有与此命令关联的打开的DataReader,必须先关闭它".

现在呢?


大家好.

我想在注册表格中更新密码.以下是一些详细信息:-

1)我有一个只有2个文本框的注册表格,1)OldPassword 2)NewPassword.没有用户名
2)我有一个只有密码的登录屏幕.没有用户名
3)我想要的是在注册表格中我应该能够将旧密码更新为新密码
4)手动插入的数据库中已经有密码.
5)数据库称为NumberPlate,表称为LoginUser,表中只有一个原始名称,称为Password

我的问题是iam收到一条错误消息,提示连接未关闭.连接的当前状态为打开".

现在我不明白问题是这样,这就是我的所有代码.不要忘记阅读最后一部分.

NEW: I removed the second open connection but now i am getting another error which says "There is already an open DataReader associated with this Command which must be closed first"

Now what???


Hello everyone.

I want to update my password in my registration form. Here are some details:-

1) I have a registration form with only 2 textbox, 1) OldPassword 2) NewPassword. No username
2) I have a login screen with only Password. No username
3) What i want is that in my registration form i should be able to update my old password to a new one
4) Theres already a password in the database inserted manually.
5) Database is called NumberPlate, table is called LoginUser, And theres only one raw in the table called Password

My problem is that iam getting an error saying "The connection was not closed. The connection''s current state is open"

Now i dont understand were the problem is so heres all my code. Dont forget to read the last part.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Number_Plate
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        SqlDataAdapter da = new SqlDataAdapter();
        SqlConnection sc = new SqlConnection("Data Source=MAAZA-PC;Initial Catalog=NumberPlates;Integrated Security=True");

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                sc.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }
            SqlCommand cmd = new SqlCommand("SELECT * FROM LoginUser", sc);
            cmd.Connection = sc;
            SqlDataReader reader = null;
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (TxtOldPassword.Text == (reader["Password"].ToString()) && TxtPassword.Text.ToString().Length > 0)
                {
                    int x;

                    da.UpdateCommand = new SqlCommand("UPDATE LoginUser SET PASSWORD=@Password", sc);
                    da.UpdateCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = TxtPassword.Text;
                    //sc.Open();
                    x = da.UpdateCommand.ExecuteNonQuery();
                    sc.Close();

                    if (x >= 1)
                    MessageBox.Show("Password reset");
                }
                else
                {
                    MessageBox.Show("Password not reset");
                }
            }
        }
    }
}




1)告诉我如何解决问题.如果您使用代码示例,这对我来说很容易
2)告诉我这段代码会起作用吗?
3)我愿意接受所有改进



Please
1) Show me how to solve the problem. It would be easy for me if you use code examples
2) Tell me is this code going to work
3) Iam open to all improvements

推荐答案

您将两次打开连接.
一次
You are opening the connection twice.
Once
try
{
    sc.Open();
}


然后


And then

sc.Open();



只需一个打开的连接语句.

否则,您可以检查您的连接是否已打开-If sc.State = adStateOpen.



Only one open connection statement is required.

Or else you can check if your connection is already open - If sc.State = adStateOpen.


这篇关于在C#中更新密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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