How to make reset form with the help of c# and sql server

Total Post:24

Points:168
C#  SQL Server  SQL 
C#
 946  View(s)
Ratings:
Rate this:
Hi Guys

I need  to make reset form  with the help of c# and sql server.Which include user id ,old password,new password and hint.
i need to show error massege for all possible error like password mismatch etc.

I would really appreciate your help

Thanks




  1. Post:24

    Points:168
    Re: How to make reset form with the help of c# and sql server

    Hi Alisha,


    I have created similar form during my training period, it may help you.




    Before going to coding part you need to create table which include all above item. I have developed the logic with the help of store procedure.

    I have created store procedure 

    Create PROCEDURE User_Info
    (
    @UserId varchar(50),
    @Old_Password varchar(50),
    @New_Password varchar(50),
    @confirm_new_password varchar(50),
    @Password_Hint varchar(50)
    )
     
    AS
    BEGIN
     
             IF NOT EXISTS (SELECT UserId FROM User_information
                             where  UserId = @UserId)
                         BEGIN
                                   PRINT 'No Such UserId Exist'
                         END
            ELSE
            begin  
            declare @checkpass varchar(30);
                                 set     @checkpass= (SELECT User_Password FROM User_information where  UserId = @UserId)
                                                              if (@Checkpass<> @Old_Password)
                                                              begin
                                                              print 'old password is wrong'
                                                              end
                                                             
                                                              ELSE
                                                                      IF (@New_Password <> @confirm_new_password)
                                                                                          BEGIN
                                                                                          PRINT 'Password doesn''t match'
                                                                                          END
                                                                                 ELSE
                                                                                          BEGIN
                                                                                          UPDATE  User_information
                             SET User_Password= @confirm_new_password , Password_Hint= @Password_Hint
                                                                                          WHERE UserId = @UserId
                                                                                 END
                       END  
    END


    Here is the complete code for the reset form:

     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    namespace FormsApplication1
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = (@"Data Source=msclient-012\sqlexpress;Initial Catalog=testing_st;User ID=sa;Password=mindstick");
                con.Open();
                SqlCommand cmdreset = new SqlCommand("User_Info", con);
                cmdreset.Connection = con;
                cmdreset.CommandType = CommandType.StoredProcedure;
                cmdreset.Parameters.AddWithValue("@Old_Password", textBox1.Text);
                cmdreset.Parameters.AddWithValue("@UserId", textBox5.Text);
                cmdreset.Parameters.AddWithValue("@New_Password", textBox2.Text);
                cmdreset.Parameters.AddWithValue("@confirm_new_password", textBox3.Text);
                cmdreset.Parameters.AddWithValue("@Password_Hint", textBox4.Text);
     
                if (textBox5.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Userid required","Warning Message");
     
                }
                else
                {
                    if(textBox1.Text.Trim() == string.Empty)
                    {
                        MessageBox.Show("Old Password Required!!","Warning Message");
     
                    }
                    else
                    {
                        if(textBox2.Text.Trim() == string.Empty)
                        {
                            MessageBox.Show("New password required","Warning Message");
                        }
                        else
                        {
                            if (textBox3.Text.Trim() == string.Empty)
                            {
                                MessageBox.Show("Confirm Password Required","Warning Message");
                            }
                            else
                            {
                                cmdreset.ExecuteNonQuery();
                                SqlCommand cmd = new SqlCommand("Select UserId from User_information where UserId=@UserId and User_Password=@User_Password", con);
                                //   SqlCommand cmd = new SqlCommand("Login_Check_Sp", con);
                                // cmd.CommandType = CommandType.StoredProcedure;
                                DataTable dt = new DataTable();
                                cmd.Parameters.AddWithValue("@UserId", textBox5.Text);
                                cmd.Parameters.AddWithValue("@User_Password", textBox2.Text);
                                SqlDataReader read = cmd.ExecuteReader();
                                read.Read();
     
                                if (read.HasRows)
                                {
                                    MessageBox.Show("Successfully changed the Password ","Contrats Message");
                                    read.Close();
                                    this.Close();
                                }
                                else
                                {
                                    read.Close();
     
                                    SqlCommand cmd1 = new SqlCommand("Select UserId from User_information where UserId=@UserId and User_Password=@User_Password", con);
                                    //   SqlCommand cmd = new SqlCommand("Login_Check_Sp", con);
                                    // cmd.CommandType = CommandType.StoredProcedure;
                                    DataTable dt1 = new DataTable();
                                    cmd1.Parameters.AddWithValue("@UserId", textBox5.Text);
                                    cmd1.Parameters.AddWithValue("@User_Password", textBox1.Text);
                                    SqlDataReader read1 = cmd1.ExecuteReader();
                                    read1.Read();
     
                                    if (read1.HasRows)
                                    {
                                        //here you may redirect to another page like Response.Redirect("Page2.aspx");
                                        MessageBox.Show("Password mismatich ","Warning Message");
                                        read1.Close();
     
                                    }
                                    else
                                    {
                                        MessageBox.Show("Old Password is wrong","Warning Message");
                                    }
                                }
     
                               
                                con.Close();
                              
                            }
                        }
                    }
                }
            }
     
               
     
            private void button2_Click  (object sender, EventArgs e)
            {
                this.Close();
            }
     
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                textBox1.PasswordChar = '*';
                textBox1.MaxLength = 8;
            }
     
            private void textBox2_TextChanged(object sender, EventArgs e)
            {
                textBox2.PasswordChar = '*';
                textBox2.MaxLength = 8;
            }
     
            private void textBox3_TextChanged(object sender, EventArgs e)
            {
                textBox3.PasswordChar = '*';
                textBox3.MaxLength = 8;
            }
     
        }
    }


      Modified On Apr-11-2018 05:37:03 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!