How to make Registration and Login Page in ASP.NET

Hi Friends!Today, i am going to make a Custom  Registration and Login Page.This is not a simple web form page.In this Form i have used many concepts.You can easily implement this concept to any where in .NET Application. In this application i have covered all things which is required in various Registration and Login Form in any  website.I am really saying to you,if you run this application on your computer then you will feel how much good it is.You can download this application from bottom and run on your visual studio.There are some controls which i have used in this application.
There are some steps to make this application.Please follow this.
Step 1: First open your visual studio->File->New->website->ASP.NET Empty website->click OK.->Now open Solution Explorer->Add New Item-> Web form-> click Add.
Step 2: Now make a Registration page like this,which is given below.
see it:

Make this Registration Form in following ways:
  • First put ScriptManager on your form.
  • Create UserName-->Now put UpdatePanel-->Now put TextBox in UpdatePanel->Put RequiredFieldValidator.
  • Password--> TextBox-->Now put RequiredFieldValidator.
  • Retype Password-->TextBox-->Now put RequiredFieldValidator.
  • Mobile Number-->TextBox-->Put RegularExpressionValidator &  RequiredFieldValidator
  • Email Id -->TextBox-->Put RegularExpressionValidator and  RequiredFieldValidator.
  • Captcha Image-->To know more here
  • Enter Captcha Image-->TextBox-->Put Label and RequiredFieldValidator.
Now go propeties in every RegularExpressionValidator &  RequiredFieldValidator and set following fields which is given below:
Control To Validate -->Select TextBox which you want to validate. open this application on your visual studio and see all changes.This is more easy for you.
Step 3: Now Add Database(.mdf) on your website.Open Solution Explorer -->Right click on website-->Add New Item-->Sql Server Database-->click Add.

Now if you are facing problem in adding database(.mdf) on Website,please here.
Step 4: Now Double click  on Database.mdf --> Solution Exporer will open-->Right click on Tables -->Add New Table-->Now Enter the column name.
see it:

Step 5: Now double click on Submit Button and write the following code which is given below:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {  
        SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
        con.Open();
        SqlCommand cmd = new SqlCommand("select*from regform where username='" + TextBox1.Text + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            Label1.Text = "User Name is Already Exist";
            this.Label1.ForeColor = Color.Red;
        }
        else
        {
            Label1.Text = "UserName is Available";
            this.Label1.ForeColor = Color.Red;
        }
        con.Close();
    }
    protected void  Button1_Click(object sender, EventArgs e)
{
    captcha1.ValidateCaptcha(TextBox6.Text.Trim());
    if (captcha1.UserValidated)
    {   //you can use disconnected architecture also,here i have used connected architecture.
        SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
        con.Open();
        String str = "insert into regform values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();

        Session["name"] = TextBox1.Text;
        Response.Redirect("default.aspx");
        con.Close();
    }
    else
    {
 Label2.ForeColor = System.Drawing.Color.Red;
 Label2.Text = "You have Entered InValid Captcha Characters please Enter again";
  }     
 }   
 }

Step 6: Now create a New page and make a Login form which is give below:
see it:
Note->In this application  all (* )represents the Label control.
Step 7: Now Double click on Login Button and write the following codes which is given below:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Drawing;

public partial class login : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
        con.Open();
        SqlCommand cmd = new SqlCommand("select COUNT(*)FROM regform WHERE username='" + TextBox1.Text + "' and password='" + TextBox2.Text + "'");
        cmd.Connection = con;
        int OBJ = Convert.ToInt32(cmd.ExecuteScalar());
        if (OBJ > 0)
        {
            Session["name"] = TextBox1.Text;
            Response.Redirect("default.aspx");
        }
        else
        {
            Label1.Text = "Invalid username or password";
            this.Label1.ForeColor = Color.Red;
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx");
    }
}

Step 8: Now Create a another page (Default.aspx)which is given below:
see it:

Step 9: Now Double click on Page and write the following codes  on Page load which is given below:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["name"].ToString();
    }
}

Step 10: Now Run the program(Press F5).
see it:

There are some points to see the Exact output of whole application.
     1. If you are not part of this application then click New User Button.
   see it:
   

     2. When you will Enter UserName then this user name will check from database.if this name is available then it will show User Name is available otherwise show User Name is Already Exist.
  see it:


     3.  when you will Enter Password & ReType Password  Field ,if both Password are not matched then see it:

   
      4. when you will Enter Mobile Number Field ,if Number is Not correct (according to India).you will see following output:
 
 
      5. If you will Enter Email Id Field and Email is not correct format then you will see following error.


     6.when  you will enter wrong Captcha code then you will see following output:

 

     7. When you will enter all fields in Registration page correct then you will see following output:
 
 Step 11: Now you will see Rajesh has  entered Database(.mdf).
see student table:


  
   Step 12: Now you can directly Login  with correct Login Name and Password,which is present in Database(.mdf).
see it:

see output:-


Note- If you want to run this application on your system directly, without any changes then follow first two steps(1 to 2)  and other four steps(3 to 6),you can add extra features in your application which are given below:
  1. First download this application form bottom and open this application to your visual studio 2010.
  2. Now go Tools -->Options -->Database Tools -->Data connections -->Remove Sql Server Instance Name(blank for default) from right hand side-->click OK.
  3. Forget Password codes
  4. You can easily send email service free to his customer after complete registration process.  
  5. If you want ,you can implement Form Based authentication in this form for security purpose.
  6. You can use different connection strings for your registration and loin page also from here. Such as store procedure,connected architecture,disconnected architecture parameterized connection etc.It will provide full security to your asp.net application.
 I hope this helpful for you.
If any problem please comment it.I will solve as soon as possible.
To Get the Latest  Free Updates Subscribe
Click below for download whole Application
          Download

68 comments:

  1. hi...
    really good article but can u tell me i want to place another button and change/refresh image on click how to do it???

    thnx in advance

    ReplyDelete
  2. @Vivek sharma
    bro.. use AdRotator control on page.visit:
    http://www.msdotnet.co.in/2013/07/web-forms-controls-in-aspnetii.html

    ReplyDelete
  3. forget link code is not avail

    ReplyDelete
  4. @Akash Raina I have checked ,it is working........
    Try again.....

    ReplyDelete
  5. thank you very much Ramashanker Verma this has really helped me for my final project as i am preparing the registration web

    ReplyDelete
  6. what about forgot password code ?

    ReplyDelete
    Replies
    1. i will explain it in separate tutorial........wait

      Delete
  7. whenever i click on submit or login button i am shown this
    can u please help?
    Server Error in '/WebSite4' Application.
    A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

    Source Error:


    Line 15: {
    Line 16: SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
    Line 17: con.Open();
    Line 18: SqlCommand cmd = new SqlCommand("select COUNT(*)FROM regform WHERE username='" + TextBox1.Text + "' and password='" + TextBox2.Text + "'");
    Line 19: cmd.Connection = con;

    ReplyDelete
    Replies
    1. @Raer ,your visual studio connection setttings and sql server instance may not same.please follow this tutorials from start to end and implement it on your sql server and visual studio as shown in tutorial. you will definitely solve your problem. visit:-
      http://www.msdotnet.co.in/2013/04/how-to-solve-problem-to-add-sql.html

      Delete
  8. the problem is in con.open()
    Also when i try to view default.aspx in browser it show this
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:


    Line 8: protected void Page_Load(object sender, EventArgs e)
    Line 9: {
    Line 10: Label1.Text = Session["name"].ToString();
    Line 11: }
    Line 12: }


    Source File: c:\Users\jc\Documents\Visual Studio 2010\WebSites\WebSite4\Default.aspx.cs Line: 10



    with the problem being in the label1.text line
    Pls help

    ReplyDelete
    Replies
    1. @Raer, first store the session value in session object on first page as
      session["name"]=TextBox1.Text; after that show the session value on the second page as Label1.Text = Session["name"].ToString(); .You can not open the second page as a startup page first.it will give error.so make "set as startup page" first web form .
      more knowledge about session visit:
      http://www.msdotnet.co.in/2013/10/how-to-use-session-state-in-aspnet.html

      Delete
    2. Amazing and useful one ... i really support u and i will refer the students

      Delete
    3. i want login with smaleest vaue in database
      for supose
      ser value
      1 15
      2 12
      3 10
      i want login smalll value in vaue colum

      Delete
  9. plz help me out m getting this kind of error
    Unable to open the physical file "C:\Users\PRIYANKA\Documents\Visual Studio 2010\WebSites\LOGIN AND USER REGISTRATION FORM\App_Data\Database.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".
    An attempt to attach an auto-named database for file C:\Users\PRIYANKA\Documents\Visual Studio 2010\WebSites\LOGIN AND USER REGISTRATION

    ReplyDelete
    Replies
    1. hello priyanka copy all code for your login and regestration form and new site create in paste code and check the your error...oterwise set the path of storage file......

      Delete
  10. How to create a web services?

    ReplyDelete
  11. Its really help full thanks bro.........

    ReplyDelete
  12. @priya Jadhav sorry for late reply.This error is coming due not open Database.mdf file.To solve this problem see this post:-http://www.msdotnet.co.in/2013/04/how-to-solve-problem-to-add-sql.html

    ReplyDelete
  13. @Sushma Rani i will discuss this our coming tutorials wait.........

    ReplyDelete
  14. give me code for forgot page plz.i really need it...........

    ReplyDelete
    Replies
    1. visit:- http://www.msdotnet.co.in/2013/11/how-to-send-forget-password-is-aspnet.html

      Delete
  15. HI, select count (*) from Table_name command is always fetch the record and redirect to the specified page.

    ReplyDelete
    Replies
    1. if you want to stay same page then delete below code
      Response.Redirect("default.aspx");

      Delete
  16. in login page it shows below type of error..
    this is code
    int OBJ = Convert.ToInt32(cmd.ExecuteScalar());
    error is : Input string was not in a correct format.

    ReplyDelete
  17. Hi Dear Friend.


    I would like to make customized outlook web access page.

    But I don't. So please help me.

    ReplyDelete
  18. i m facing this problem plz help me
    The system cannot find the file specified
    Line 19: SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
    Line 20: con.Open();
    Line 21: SqlCommand cmd = new SqlCommand("select COUNT(*)FROM regform WHERE username='" + TextBox1.Text + "' and password='" + TextBox2.Text + "'");
    Line 22: cmd.Connection = con;

    ReplyDelete
  19. you are facing connection problem.read below link;-
    http://www.msdotnet.co.in/2013/04/how-to-solve-problem-to-add-sql.html

    ReplyDelete
  20. mind bloing..............
    awasaaaaaaaammmmmmmmmmmmm

    ReplyDelete
  21. how the registration page more attractive?that is stylish textboxes,button etc.

    ReplyDelete
  22. HI HOW TO UPLOAD An photo and display in same from n in database

    ReplyDelete
  23. i have download ur project and try to open there no file with extension .slm. but i have open each file one after the other and when i ty to run it is showing me to attachment some what is that ?
    yes i am new to asp.net and want to learn from you please help me..............

    ReplyDelete
  24. Hi Mr. Ramashanker Verma

    This work is so very helpful.
    How can I verify my registration using the email id i used for registration.. .do you have a code for it (email verification) ? Thank you for the help.

    ReplyDelete
  25. do you also able to generate auto suggest username like in registation yahoo.com .. may you help me pls. thank you i need my school project

    ReplyDelete
  26. hello sir,
    how are you?
    how to add profile image in profile and how to set http address like in facebook.com and facebook.com/zakee.malik.. please help me

    ReplyDelete
  27. hello sir , i have completed all coding and design for registration form , but its does not work , its application run , does not redirect to another page and user details like user name, mob num, email id did not insert into table. i dont know what is my mistake ,
    i want to implement my project ,,,,, i need ur comment..

    ReplyDelete
    Replies
    1. hi kalpana,what error is showing,when you click button,then i can tell you what is actual problem in your application.........

      Delete
    2. when i click submit btn , does nt show any error ,its simply on same page olny.. data did nt insert into table and did nt direct

      Delete
  28. Hi plz help me.. I want to create login page where i enter a name as username then login... when I enter a login then it can display me a homepage... and in homepage there will b another button logout... plz tell me the code.... as soon as possible(using session)

    ReplyDelete
    Replies
    1. sorry for late reply,i have not seen you comment.
      session["variable_name1"]=null;
      session["variable_name2"]=null;
      response.redirect("home.aspx");
      Or
      session.removeall();
      response.redirect("home.aspx");

      Delete
  29. hi i followed all proceduer as you gave in add capcha image but i got Error like "The type or namespace name 'MSCaptcha' could not be found in the global namespace (are you missing an assembly reference?)".So please can you give mi solution on this?

    ReplyDelete
  30. Hi kalpana ,your handler is not working correctly.first download the application -->open your visual studio 2010-->only change connection strings otherwise read http://www.msdotnet.co.in/2013/04/how-to-solve-problem-to-add-sql.html
    this then you will not have to change connection strings.

    ReplyDelete
    Replies
    1. thanx a lot finaly i got d o/p ....

      Delete
    2. thanx a lot finally got d output

      Delete
  31. Hi..Sir.
    Please tell me that how to use filter for data searching without database. Actually i have to call API.

    ReplyDelete
  32. hello sir,
    i've followed all the code you posted
    but i got some problem on the checking of the username
    it doesn't show any error in checking the username but the label doesn't automatically show the text which notify that the username is available. I've put the textbox in the update panel though
    can u give me some advice? thx

    ReplyDelete
    Replies
    1. hi Alvin , go properties set AutoPostBack = True.then will work .........

      Delete
  33. thank you so much.......................

    ReplyDelete
  34. How to use ms access database in this program????

    ReplyDelete
  35. Y don't u reply n my comment???

    ReplyDelete
    Replies
    1. hi Rumaisa see it,in your first comment......

      Delete
  36. I want to connect my login page to Sql database ...How can i do that ?and more how can i know if i enter some values in login page or registration page can we know whether it is storing in our database or not? how can we know that/ will u please explain me

    ReplyDelete
    Replies
    1. hi shalini !
      1.) in above post i have used Database.mdf file for login. you can login with sql database or other database by providing connection strings.i have mentioned different connection strings with database,you can see from below link.
      http://www.msdotnet.co.in/2013/12/how-to-use-different-connection-strings.html
      2.) If you want check whether data is inserted in database or not .open solution explorer-->open Database.mdf file --> Tables--> right click on your tables name ---> Show Table Data.you can see data is inserted or not.other things if data is not inserted in table then it will give error.
      3.) if want to attached Database.mdf or .sdf file in your sql server then read below links.
      http://www.msdotnet.co.in/2014/03/how-to-attached-mdf-or-sdf-file-in-sql.html

      first read above links carefully ,if any other problem ask frequently......

      Delete
  37. hello.what is the use of update panel and script manager??????

    ReplyDelete
    Replies
    1. Read below link:
      http://www.msdotnet.co.in/search/label/Ajax

      Delete
    2. thanks dude.............

      Delete
  38. i am getting error.please help
    the page says: Server Error '/' Application
    Webforms UnobstructiveValidationMode requires a ScriptResourseMapping For 'jquery'.Please add a ScriptResourseMapping names jquery(case-sensitive).

    ReplyDelete
  39. Hi sir i run that program successfully but when i enter the detail on registration page then the database connection error will show the visual studio open and cursor on open con and that error come on the mobile entry

    ReplyDelete
    Replies
    1. Go step 5-->check Text Box change connection string codes-->this connection strings are showing errors-->correct it then it will work....

      Delete
  40. Hi sir i successfully run that registration page but when i entry n registration page so the database error come when i entry in the mobile no text box that time visual studio show the cursor on con.Open(); command so please help me how i resolve that problem

    ReplyDelete
    Replies
    1. hi Vikash, your connection string are not working properly.first correct it by reading the above tutorials carefully.

      Delete
  41. can we give primary key with identity??

    ReplyDelete
  42. hi komal ! Yes ,you can set a primary key in your database column...

    ReplyDelete
  43. Hi Verma,

    I want to create a registration page with first name,last name,cellphone number and prefered contact whould be email or text.
    Please help me in the coding.
    If user click the SUBMIT BUTTON.
    The details should store in sql.
    Please help me .

    Thank you,
    Sowjanya.

    ReplyDelete
    Replies
    1. hi sowjanya ! In above tutorial ,you can store all values in sql database by click submit button..Here i have used .mdf database which is a part of sql database.you can store your values in separated database also if you want. you can follow below link for connection string in separate sql database .
      http://www.msdotnet.co.in/2013/12/how-to-use-different-connection-strings.html

      Delete

@2012-2014 msdotnet. All Rights Reserved. Don't Copy