不知道为什么 总是无法提示修改密码 总显示原密码错误 不知道问题出哪了
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.SqlClient;namespace SuperMarket
{
    public partial class ModificationPassword : Form
    {
        public ModificationPassword()
        {
            InitializeComponent();
        }        /// <summary>
        /// 修改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (Modification())
            {
                if (Chose())
                {
                    MessageBox.Show("修改密码成功!");
                }
                else
                {
                    MessageBox.Show("密码修改失败!");
                }
            }
        }
        public string username;
        public string passwrd;        /// <summary>
        /// 修改密码的方法
        /// </summary>
        /// <returns></returns>
        public bool Modification()
        {   
            if (this.textBox1.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入原密码!");
                this.textBox1.Focus();
                return false;
            }
            if (this.textBox2.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入新密码!");
                this.textBox2.Focus();
                return false;
            }
            if (this.textBox3.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入确认新密码!");
                this.textBox3.Focus();
                return false;
            }
            if (this.textBox2.Text != this.textBox3.Text)
            {
                MessageBox.Show("新密码输入不一致!");
                return false;
            }
            else
            {
                return true;
            }
        }        public bool Chose()
        {
            DBHelper.OpenConnection();
            string sql = string.Format("select count(*) from [Users] where UserName='{0}' and PassWord='{1}'",username,this.textBox1.Text.ToString());
            SqlCommand com = new SqlCommand(sql, DBHelper.Connection);
            try
            {
                int count = (int)com.ExecuteScalar();
                if (count > 0)
                {
                    sql = string.Format("update [Users] set PassWord='{0}' where UserName='{1}'", this.textBox2.Text.ToString(), username);
                    com.CommandText = sql;
                    if (com.ExecuteNonQuery() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    MessageBox.Show("原密码错误");
                    return false;
                }
            }
            catch (Exception)
            {                throw;
            }
            finally
            {
                DBHelper.CloseConnection();
            }
        }
        /// <summary>
        /// 返回事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("你是否要返回商品管理界面?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
            if (result==DialogResult.Yes)
            {
                this.Hide();
                SuperMarketManage sp = new SuperMarketManage();
                sp.Show();
            }
        }    }
}
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.SqlClient;namespace SuperMarket
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 登录时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (ChickMessage())
            {
                if (ChickLogin())
                {
                    MessageBox.Show("登录成功!");
                    this.Hide();
                    SuperMarketManage spu = new SuperMarketManage();
                    spu.Show();
                }
                else
                {
                    MessageBox.Show("帐号或者密码错误!请重新输入!");
                }
            }
        }        /// <summary>
        /// 验证登录的方法
        /// </summary>
        public bool ChickLogin()
        {
            bool flag = false;
            ModificationPassword mp = new ModificationPassword();
            mp.username = this.textBox1.Text.Trim().ToString();
            mp.passwrd = this.textBox2.Text.Trim().ToString();
            string sql = string.Format("select count(*) from Users where UserName='{0}' and PassWord='{1}'",mp.username,mp.passwrd);
            
            try
            {
                DBHelper.OpenConnection();
                SqlCommand com = new SqlCommand(sql,DBHelper.Connection);
                int count = (int)com.ExecuteScalar();
                if (count>0)
                {
                    flag = true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                DBHelper.CloseConnection();
            }
            return flag;
        }
        /// <summary>
        /// 验证是否有输入
        /// </summary>
        /// <returns></returns>
        public bool ChickMessage()
        {
            if (this.textBox1.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入帐号!");
                this.textBox1.Focus();
                return false;
            }
            if (this.textBox2.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入密码!");
                this.textBox2.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }        public void Exit()
        {
            DialogResult result = MessageBox.Show("你是否取消登录并退出!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (result==DialogResult.Yes)
            {
                Application.Exit();
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            Exit();
        }
    }
}

解决方案 »

  1.   

         string sql = string.Format("select count(*) from [Users] where UserName='{0}' and PassWord='{1}'",username,this.textBox1.Text.ToString());
     
    在调试过程中,将这句放到查询分析器里看看返回的结果是不是大于0,如果等于0,说明输入的用户名或密码与数据库中的不一致
     
      

  2.   

    你的ModificationPassword页面,自始自终就没对那个变量username初始化过,一直为空,你倒底要修改谁的密码,它怎么会知道?
      

  3.   

    “找错”是程序员和debugger的事情,不是烧香求“大神”。学会怎么单步调试,怎么缩小问题的范围,如何编写测试用例覆盖代码路径并且发现错误,如何审读代码,比一摊手求“大神”靠谱多了。没有哪个大神愿意做给菜鸟擦屁股的事情。