Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,guys

I was just playing around c#.net as a new student,i did wanted to make billing system and initially i tried with updating/changing password of the username,scene is when user logins ,entering his/her username password,then there is a option to change the password via settings like in general yahoo,hotmail change password system.i hab sql db and it is connected but when i try to change the password it doesnt shows error,message says "it has been change" but when i check db password remains same, it doesnt change,like when i try to change the password of username:login whose present password is login1 to login2 ,it doesnt change.

The other fields in the form are username,password ,new password,confirm password,and the coe shown below is the update button click event.

Here is the coding I have done. Help me i couldnt trace the mistake/error.


C#
namespace Restaurant_Billing 
{
    public partial class changepassword : Form
    {
        public changepassword()
        {
            InitializeComponent();
        }
        SqlConnection cn = new SqlConnection();
        SqlCommand cm = new SqlCommand();
        

        private void button2_Click(object sender, EventArgs e)
        {
               Close();
                     
        }


        private void button1_Click(object sender, EventArgs e) //updating the new [
        {
            txtusername.Text = globalclass.m_globalvar;
            txtpasscurrent.Text = globalclass.m_globalvar;

            string username = txtusername.Text;
            string password = txtpasscurrent.Text;
            string newpassword = textpassnew.Text;
            string confirmnewpassword = textpassconfirm.Text;
            if (textpassnew.Text == textpassconfirm.Text)
            {
                cm = new SqlCommand("update tbluser set Pw = '" + textpassnew.Text + "' where U_name = '" + txtusername.Text + "'");
                cm.Connection = cn;
                cm.ExecuteNonQuery();
                MessageBox.Show("password has been changed");
            }
            else
            {
                MessageBox.Show("some errors!! dawg correct it!!");
                txtpasscurrent.Focus();
            }
        }

            private void changepassword_Load(object sender, EventArgs e)
        {
            this.txtusername.Text = globalclass.m_globalvar;
            cn = new SqlConnection(@"server=UMESHINATOR-PC\SQLEXPRESS;integrated security=true;database=nec");
            cn.Open();
        }
    }
}
Posted
Updated 12-Aug-12 8:27am
v2
Comments
Rock (Multithreaded) 12-Aug-12 15:14pm    
One important thing is,
int returned=cm.ExecuteNonQuery();
if(returned==1)
{
MessageBox.Show("password has been changed");
}
else
{
MessageBox.Show("Nothing has been changed");
}
Rock (Multithreaded) 12-Aug-12 15:29pm    
Your query was wrong!
Now New Query is:
cm = new SqlCommand("update tbluser set Pw = 'NewPassword' where Pw = 'OldPassword' And U_name= 'UserName';
StianSandberg 12-Aug-12 17:05pm    
Your code is vulnerable for sql-injectons. You should use sql parameters. I wrote an article about this a few weeks ago: read it here

Your query was wrong!
Now New Query is:
cm = new SqlCommand("update tbluser set Pw='"+newpassword+"' where Pw='"+password+"' And U_name='"+username+"'");
 
Share this answer
 
v2
What is the primary key of your database?Is it U_name?Because you search your database here by U_name in your update statement.Then if you type such an U_name in txtusername that it isn't present in your database then you will find no update in your database.You can ignore it by making a candidate key(username+newpassword) or make sure every value of U_name of your database is unique and as same as the text of txtusername.
 
Share this answer
 
Comments
Rock (Multithreaded) 12-Aug-12 21:50pm    
He wants to update only password on same user.
meshinator 14-Aug-12 7:05am    
Thanks everyone for ur kind and valuable suggestions ..ill try,what u guys hab suggested and since im a new programming insect..if i get any problems again ill get back to bite you guys..hope no worries. :)

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