Skip to main content

How To Create A Change Password Form in a C# Application

Hello Geeks .. This little post is a free sample C# Windows Form that can be used to change password in a SQL Database .

In this Sample you will find a class called ChangeInDB.cs and Other Called Connection.cs I use those two classes in the process of connecting to Database.Open Them and Change the Connection String to be the One for your own computer .

Link To Download The Working Sample : Change Password Win Form

Steps To Implement Change Password Form Inside your Applications : 

  1. Create a SQL Database Table That has UserID and Password Fields and Add Some Records.
  2. Create a class to implement connections to Database and methods of communicating with your Database . 

  3. Add Those Methods Inside It
    public DataTable RunQuery(string select)
    {
         SetComnd(select);
         myTable.Load(cmd.ExecuteReader());
         return myTable;
    }
    public bool login(string user, string pass)
    {
         connect();
         string sqlcmd = "select * from LoginInfo where ID='" + user + "' and Password='" + pass + "'";
         DataTable mytbl = RunQuery(sqlcmd);
         if (mytbl.Rows.Count > 0) return true;
         else return false;
    }
    private void connect()
    { cn.Open(); }

  4. Implement This Code Under The ButtonConfirm_Click Event
    private void btnConfirm_Click(object sender, EventArgs e)
    {
         SqlCommand cmd = new SqlCommand();
         SqlConnection cn = new SqlConnection(Connection.myConnectionString);
         ChangeInDB changePass = new ChangeInDB();
         string strCmd ="update LoginInfo set Password='"+txtNew.Text+"' where ID='"+txtUser.Text+"'";
         if (changePass.login(txtUser.Text, txtOld.Text) == true)
         {
              if (txtNew.Text == txtVerify.Text)
              {
                 try
                {
                  cn.Open();
                  cmd.Connection = cn;
                  cmd.CommandText = strCmd;
                  cmd.ExecuteNonQuery();
                  MessageBox.Show("Done Successfully!");
                }
                catch (Exception ex)
                {
                   MessageBox.Show(ex.ToString());
                }
             }
       else
          MessageBox.Show("Passwords Does Not Match");
        }
     else
      MessageBox.Show("User/Password Incorrect!");
    }

  5. Add The Features you want in your form such as minimum & maximun set of chars for password  ... Etc .

You May Also Read :

Comments

Popular posts from this blog

C# Full Pharmacy Project With Source code

Hello Geeks .. This post is one of our completely Free Projects  .. It is a Pharmacy Project that can perform a lot of operations you can also find the (.dll) files for the working Barcode Reader Machines in your country and add it to the project .

Programming Languages Ranking | Feb 2015 | JavaScript Greatest Performance

This is my second post along the blog speaking about programming languages ranking according to  TIOBE  for software quanlity. Before heading off to speak about the ranking, I would like to point out that this rank is based on the amount of searches and content availiable on the web for each programming language, You can read the full definition of TIOBE index  here .