Thursday, June 23, 2011

How to change Login password and user name using with SQL database?

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  change_password_and_user name
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=user; User Id=sa; Password=ranjeet");


       
       
        
        private void button1_Click(object sender, EventArgs e)
        {


             try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("Select * from check1 where username ='" + textBox1.Text + "'", con);

               
                SqlDataReader dr = cmd.ExecuteReader();
               

                if ((dr.Read()) & textBox2.Text == textBox3.Text & textBox3.Text!="")
                {
                    con.Close();
                    con.Open();
                    string str = "update check1 set username=@username,uhints=@uhints where username='" + textBox1.Text + "'";
                    SqlCommand cmd1 = new SqlCommand(str, con);
                    cmd1.Parameters.Add(new SqlParameter("@username", (object)textBox3.Text));
                    cmd1.Parameters.Add(new SqlParameter("@uhints", (object)textBox8.Text));

                    cmd1.ExecuteNonQuery();

                    MessageBox.Show("User Name change Successfully");
                    Form1 r = new Form1();
                    r.Show();
                    this.Hide();
                    textBox1.Clear();
                    textBox2.Clear();
                    textBox3.Clear();
                    textBox8.Clear();
                   
                    con.Close();

                      

                   

                }

                else if (textBox2.Text != textBox3.Text)
                {
                    MessageBox.Show("The user name and confirmation user name don not match!");
                 
                    textBox2.Clear();
                    textBox3.Clear();
                  
                }


                else if (textBox1.Text == "" & textBox2.Text == "" & textBox3.Text == "")
                {
                    MessageBox.Show("Please enter old user name ,new user name and confirm user name ");
                  
                }
                else if (textBox2.Text == "" & textBox3.Text == "")
                {
                    MessageBox.Show("Please enter new user name and confirm user name");
                 
                }
                else if (textBox1.Text == "")
                {
                    MessageBox.Show("Please enter old user name");
                 
                }
                else if (textBox2.Text == "")
                {
                    MessageBox.Show("Please enter new user name");
                   
                }

                else if (textBox3.Text == "")
                {
                    MessageBox.Show("Please enter confirm user name");

                }



                else
                {
                    MessageBox.Show("The current password you have entered is not valid!");
                    textBox1.Clear();
                    textBox2.Clear();
                    textBox3.Clear();
                    con.Close();
                }
              
            }
            catch (Exception exc)
            {


                MessageBox.Show(exc.Message);



            }
            con.Close();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 t = new Form1();
            t.Show();
            this.Hide();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("Select * from check1 where password ='" + textBox4.Text + "'", con);


                SqlDataReader dr = cmd.ExecuteReader();


                if ((dr.Read()) & textBox5.Text == textBox6.Text & textBox6.Text != "")
                {
                    con.Close();
                    con.Open();
                    string str = "update check1 set password=@password,phints=@phints where password='" + textBox4.Text + "'";
                    SqlCommand cmd1 = new SqlCommand(str, con);
                    cmd1.Parameters.Add(new SqlParameter("@password", (object)textBox6.Text));
                    cmd1.Parameters.Add(new SqlParameter("@phints", (object)textBox7.Text));

                    cmd1.ExecuteNonQuery();

                    MessageBox.Show("Password change Successfully");
                    Form1 r = new Form1();
                    r.Show();
                    this.Hide();
                    textBox4.Clear();
                    textBox5.Clear();
                    textBox6.Clear();
                    textBox7.Clear();
                    con.Close();





                }

                else if (textBox5.Text != textBox6.Text)
                {
                    MessageBox.Show("The passowrd and confirmation password do not match!");

                    textBox5.Clear();
                    textBox6.Clear();

                }


                else if (textBox4.Text == "" & textBox5.Text == "" & textBox6.Text == "")
                {
                    MessageBox.Show("Please enter old password ,new password and confirm password ");

                }
                else if (textBox5.Text == "" & textBox5.Text == "")
                {
                    MessageBox.Show("Please enter new password and confirm user name");

                }
                else if (textBox4.Text == "")
                {
                    MessageBox.Show("Please enter old password");

                }
                else if (textBox5.Text == "")
                {
                    MessageBox.Show("Please enter new password");

                }

                else if (textBox6.Text == "")
                {
                    MessageBox.Show("Please enter confirm password");

                }



                else
                {
                    MessageBox.Show("The current password you have entered is not valid!");
                    textBox4.Clear();
                    textBox5.Clear();
                    textBox6.Clear();
                    con.Close();
                }

            }
            catch (Exception exc)
            {


                MessageBox.Show(exc.Message);



            }
            con.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form1 t = new Form1();
            t.Show();
            this.Hide();
        }

      
    }
}

No comments:

Post a Comment