Friday, 27 December 2013

Change password using asp.net



Introduction: Hello friends, in this article i will explain that how we can change the password after login, in asp.net.This article is very useful for the all the .net developer.

Implementation: create a new website add a page. Drag and drop three textboxes and two buttons  from the toolbox inside the  <body> body tag at the .aspx page.  Below  i am giving the complete code for the html page and .cs page. 

Code for default.aspx page
<div>
    <table style="width: 91%; height: 97px;" class="tbl_reg_form">
        <tr>
            <td>
                Old Password</td>
            <td>
                <asp:TextBox ID="txt_old_pwd" runat="server" CssClass="input" TextMode="Password"></asp:TextBox>
                &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                    ControlToValidate="txt_old_pwd" ErrorMessage="Required">*</asp:RequiredFieldValidator>
            </td>
        </tr>
      
        <tr>
            <td>
                New Password</td>
            <td>
                <asp:TextBox ID="txt_new_pwd" runat="server" CssClass="input" TextMode="Password"></asp:TextBox>
                &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                    ControlToValidate="txt_new_pwd" ErrorMessage="Required">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td style="height: 29px">
                Confirm Password</td>
            <td style="height: 29px">
                <asp:TextBox ID="txt_confirm_pwd" runat="server" CssClass="input" TextMode="Password"></asp:TextBox>
                &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                    ControlToValidate="txt_confirm_pwd" ErrorMessage="Required">*</asp:RequiredFieldValidator>
                &nbsp;</td>
        </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:CompareValidator ID="CompareValidator1" runat="server"
                        ControlToCompare="txt_new_pwd" ControlToValidate="txt_confirm_pwd"
                        ErrorMessage="CompareValidator">New Password does not match</asp:CompareValidator>
                </td>
            </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:Button ID="btn_submit" runat="server"
                    Text="Change Password" onclick="btn_submit_Click" />
                &nbsp;<asp:Button ID="btn_cancel" runat="server" CausesValidation="False"
                     Text="Reset" onclick="btn_cancel_Click" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:Label ID="lbl_msg" runat="server" Text=""></asp:Label>
            </td>
        </tr>
    </table>
    </div>

Code for default.aspx.cs page

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;  
public partial class _Default : System.Web.UI.Page
{
  
    SqlCommand cmd;
    DataTable dt;
    SqlConnection con = new SqlConnection();
    SqlDataAdapter adp;
 
    protected void Page_Load(object sender, EventArgs e)
    {



        // here i am declare connection 
        con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
        con.Open();
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
     
     
    }

    protected void btn_submit_Click(object sender, EventArgs e)
    {

        if (con.State == ConnectionState.Closed)
        { con.Open(); }
        try
        {
            // here inside the cmd  definning sql query to select the password from the table
            adp = new SqlDataAdapter("select password from REGISTRATION where  password=?password and username=?username", con);
            // Passing parameters
            adp.SelectCommand.Parameters.AddWithValue("?password", txt_old_pwd.Text);
            //  This program will be working when the user will login inside the application so you can pass parameter
            // named username via session or cokkie, here i am using session
            adp.SelectCommand.Parameters.AddWithValue("?username", Session["username"].ToString());
            dt = new DataTable();

            adp.Fill(dt);
            // Here checked the enter old password is available inside the database or no
            // If the old password not correct entered by the user, then it will enter inside the if condition
            if (dt.Rows.Count == 0)
            {
                // it will display message inside the lable
                lbl_msg.ForeColor = Color.Red;
                lbl_msg.Text = "Old Password does not match";
                clr_rec();
                return;
            }
            // If the old password  correct entered by the user, then it will enter inside the else condition
            else
            {

                // here inside the cmd  definning sql query to update the password from the table
                cmd = new SqlCommand("update REGISTRATION set password=?password where   username=?username", con);
                // Passing parameters
                cmd.Parameters.AddWithValue("?password", txt_new_pwd.Text);
                //  This program will be working when the user will login inside the application so you can pass parameter
                // named username via session or cokkie, here i am using session.
                cmd.Parameters.AddWithValue("?username", Session["username"].ToString());
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
                // it will display message inside the lable that password has changed Successfully
                lbl_msg.Text = "Password has changed Successfully";
                clr_rec();

            }
        }
        catch { }
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        clr_rec();
    }

    private void clr_rec()
    {
        // this method will crear all the mentioned textboxes.
        txt_confirm_pwd.Text = "";
        txt_new_pwd.Text = "";
        txt_old_pwd.Text = "";
    }
}


Connectionstring

<connectionStrings>
  <add name="cnn" connectionString="Data source=BHARAT-HP;database=practice;uid=sa;pwd=1234;Min Pool Size=100;Max Pool Size=1000;Connect Timeout=10"/>
  </connectionStrings>

Database Script

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[REGISTRATION]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[REGISTRATION](
      [id] [bigint] IDENTITY(1,1) NOT NULL,
      [username] [varchar](50) NULL,
      [password] [varchar](50) NULL,
 CONSTRAINT [PK_REGISTRATION] PRIMARY KEY CLUSTERED
(
      [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END

See the output inside the image below





Conclusion: In above code, I have been explained that how  we can change password  in asp.net. This code is very helpful for every .net developer. Gud bye and take care developers. We will come back shortly with the new article.



Regards
Using Asp.net





68 comments:

  1. Replies
    1. Great Article Cyber Security Projects projects for cse Networking Security Projects JavaScript Training in Chennai JavaScript Training in Chennai The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Very Nice tutorial. Really helpful for a lot of information about ASP …. Please keep update some more…………

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. 6-week summer course in Noida - 6 weeks The summer course plays a crucial role in shaping the career of young aspiring / informatics students. This training has been specifically introduced so that students can become familiar with current industrial culture and industrial needs. Webtrackker technology offers a 6-month training program for students / graduates that includes small and large projects.
    6-week summer course in Noida

    ReplyDelete
  5. Each department of CAD have specific programmes which, while completed could provide you with a recognisable qualification that could assist you get a job in anything design enterprise which you would really like.

    AutoCAD training in Noida

    AutoCAD training institute in Noida


    Best AutoCAD training institute in Noida

    ReplyDelete
  6. Video editing course in Noida
    Video editing training institute in Noida- Webtrackker Technology is and IT Training institute providing the Video editing course in Noida, FCP, Final Cut Pro Training in Noida. For more call us- 8802820025.
    Video editing course in Noida
    Company Address:
    Webtrackker Technology
    C- 67, Sector- 63, Noida
    Phone: 01204330760, 8802820025
    Email: info@webtrackker.com
    Website: http://webtrackker.com/Best-training-institute-Video-editing-FCP-course-in-Noida.php

    ReplyDelete
  7. Cloud Computing Training In Noida
    Webtrackker is IT based company in many countries. Webtrackker will provide you a real time projects based training on Cloud Computing. If you are looking for the Cloud computing training in Noida then you can join the webtrackker technology.
    Cloud Computing Training In Noida , Cloud Computing Training center In Noida , Cloud Computing Training institute In Noida ,

    Company Address:
    Webtrackker Technology
    C- 67, Sector- 63, Noida
    Email: info@webtrackker.com
    Website: www.webtrackker.com
    http://webtrackker.com/Cloud-Computing-Training-Institutes-In-Noida.php

    ReplyDelete
  8. Your blog on RPA is so attractive that i am not able to stop myself to read this blog of yours on RPA.This blog contains all the important topics and points of RPA. Requesting you keep updating this post on RPAand help them who is eager to gain knowledge on RPA.
    Thanks and Regards,
    Uipath training in chennai
    blue prism training institute in chennai
    blue prism training with certification in chennai

    ReplyDelete
  9. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Roles and reponsibilities of hadoop developer | hadoop developer skills Set | hadoop training course fees in chennai | Hadoop Training in Chennai Omr

    ReplyDelete
  10. Thank you for sharing wonderful information with us to get some idea about that content. check it once through
    Machine Learning With TensorFlow Training and Course in Tel Aviv
    | CPHQ Online Training in Beirut. Get Certified Online

    ReplyDelete
  11. WOW! Really Nice Post! I personally believe that to maintain the standard of a blog all the hacks mentioned above are important. All points discussed were worth reading
    and I’ll surely work with them all one by one.

    CEH Training In Hyderbad

    ReplyDelete
  12. If you are looking for Self learning, Distance Education Courses are becoming increasingly popular as a mode of education and are being utilized by professionals and students from around the world, there are numerous possibilities for distance learning courses. Just go through this link:-

    Distance Education Courses ,

    ReplyDelete
  13. Are you looking for Distance Learning Courses in India most of the students choose and apply, Talentedgenex there are many popular courses which attract the students for having distance education. For more info visit this site:- Distance learning courses in India ,

    ReplyDelete
  14. Talentedgenext Way of Online Learning, Distance Education, is an increasing number of becoming popular all over the world due as it has many benefits. For further details visit in this site:- Distance Education Website,

    ReplyDelete
  15. Very nice article thank you sharing it. I have one another opportunity to know about Software Development & Website Designing in Lucknow

    ReplyDelete
  16. Online BBA degree in India is one of the most popular couses for students who are keen to learn business statistics, communicating skills, marketing management, entrepreneurship and small business management, international business, etc. To know more, visit:
    Online BBA Degree in India,

    ReplyDelete
  17. Thanks for sharing such a wonderful blog on Mean Stack .This blog contains so much data about Mean Stack ,like if anyone who is searching for the Mean Stack data,They will easily grab the knowledge of from this.Requested you to please keep sharing these type of useful content so that other can get benefit from your shared content.
    Thanks and Regards,
    Mean Stack training in Chennai
    Best mean stack training in Chennai
    Top Mean stack raining in Chennai
    Course fees for Mean stack in Chennai
    Mean stack training fees in Velachery, Chennai

    ReplyDelete
  18. Great article, It's one of the best content in your site. I really impressed the post. Good work keep it up. Thanks for sharing the wonderful post.See also Best International School.
    Best CBSE School in Ballia

    ReplyDelete
  19. Great article, It's one of the best content in your site. I really impressed the post. Good work keep it up. Thanks for sharing the wonderful post.
    Best Chicken kurti

    ReplyDelete
  20. Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
    pmp course

    ReplyDelete
  21. Tula's International School is the best Dehradun boarding schools for girls & boys. It is one of the top schools in Dehradun.The school is affiliated to CBSE which offers holistic education to students.

    Tula's International School Best Boarding School in India


    Tula's International School Co-ed Boarding School in Dehradun

    Tula's International School Dehradun Boarding School Fee structure

    Tula's International School Top Girls Boarding School India

    Tula's International School Best CBSE Schools in Uttarakhand

    Tula's International School Top Boarding Schools in India

    Tula's International School Best Boarding School in Dehradun

    Tula's International School Top Boarding Schools in Dehradun

    Tula's Institute Best BCA College in Dehradun

    Tula's Institute Best BCA College in Dehradun

    ReplyDelete


  22. I just wanted to let you know that the piece you shared with us is pretty unique and authentic. I am going to keep browsing this blog.



    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery


    ReplyDelete
  23. I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here
    Salesforce Training in Chennai

    Salesforce Online Training in Chennai

    Salesforce Training in Bangalore

    Salesforce Training in Hyderabad

    Salesforce training in ameerpet

    Salesforce Training in Pune

    Salesforce Online Training

    Salesforce Training


    ReplyDelete
  24. This article is a great article that I have seen in my asp.net programming career so far.

    website development company in Surat Gujarat

    ReplyDelete
  25. Attend The PMP Certification From ExcelR. Practical PMP Certification Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The PMP Certification.
    PMP Certification

    ReplyDelete
  26. PMP Certification with placement was never so easy and adaptable to everyone but here at ExcelR We teach you numerous ways of doing PMP Courses, which are way easy and interesting. Our experienced and expert faculty will help you reach your goal. 100% result oriented strategies are being performed; we offer PMP Certification Course in pune

    PMP Certification

    ReplyDelete
  27. This site helps to clear your all query. vikram university bsc 3rd year result 2021
    vikram university pg result 2021 This is really worth reading. nice informative article.

    ReplyDelete