• Now Online : 89
  • admin@codemyne.net

Introduction

This article explains about creating a simple registration or signup and login or sign in form using asp.net, c#.net and sqlserver.

Registration form
Login form

Follow the steps as described below to create the registration form and login form as shown above.

Creating Database:

  • Create a databse in sqlserver with name 'TestDB'
  • Next create a table with fields as shown in picture.

Registration form table

Creating Registration or signup form:

  • Now create a new website using visual studio 2008 0r 2010.
  • Design the registraion form and login form as shown in above pictures. In these two forms, validation controls are used to validate the fields as per requirement. To know about validation controls in asp.net read the following article.
  • Add connection string property in web.config file as shown below.
    <configuration>
    <connectionStrings>
    <add name="CM_Connection" connectionString="data source=localhost;Initial Catalog=TestDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
    Note:In this web.config file, the connection string name is 'CM_Connection'. You can change data source to your local server name (here it is localhost).
  • Next in the 'CreateUser' button click event write the following code.
    if (Page.IsValid)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            Guid guid;
            guid = Guid.NewGuid();
            string sql = "INSERT INTO TestDB_RegisterUser (registeruser_id,registeruser_username,registeruser_email,registeruser_password";
            sql += " ,registeruser_createdon,registeruser_modifiedon,registeruser_rowstate)";
            sql += "VALUES (@ID,@Username,@Email,@Password,@Createdon,@Modifiedon,@Rowstate)";
            cmd.Parameters.AddWithValue("@ID", guid);
            cmd.Parameters.AddWithValue("@Username", UserName.Text.Trim());               
            cmd.Parameters.AddWithValue("@Email", Email.Text.Trim());
            cmd.Parameters.AddWithValue("@Password", Password.Text);
            cmd.Parameters.AddWithValue("@Createdon", DateTime.Now);
            cmd.Parameters.AddWithValue("@Modifiedon", DateTime.Now);
            cmd.Parameters.AddWithValue("@Rowstate", 1);                
            cmd.Connection = con;
            cmd.CommandText = sql;
            con.Open();
            try
            {
                cmd.ExecuteNonQuery();
                ErrorMessage.Text = "Registered successfully.";                    
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
    Note: You can check the validity of a Page by checking the Page.IsValid property. For this property to return true, all validation server controls in the current validation group must validate successfully. You should check this property only after you have called the Page.Validate method, or set the CausesValidation property to true in the OnServerClick event handler for an ASP.NET server control that initiates form processing. In simple words the Page.IsValid property tells you whether the validation succeeded or not.

In the above method after Page.IsValid, insert the record into database as shown. This finishes the registration form.

Creating Signin or Login form: In the 'Log In' button click event write the code as shown below.

 stringemail = UserName.Text.Trim();
string pwd = Password.Text;
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString))
{
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    // cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "SELECT registeruser_id,registeruser_username, registeruser_email,registeruser_password FROM TestDB_RegisterUser where registeruser_email='" + email + "' and registeruser_password='" + pwd + "' and registeruser_rowstate<3 ";
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        Session.Add("UserID", dt.Rows[0][0].ToString());
        Session.Add("Username", dt.Rows[0][1].ToString());
        Session.Add("UserEmail", dt.Rows[0][2].ToString());
        Response.Redirect("~/Default.aspx", false);
    }
    else
    {
        FailureText.Text = "Invalid username or password";            
    }
}

The above method gets the record that matches with useremail and password. Otherwise the datatable will be empty which is used to show 'Invalid username or password' to the user.

Thus you can create a registration form and log in form using asp.net

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment

sazee 11/21/2012 (IST) / Reply

Help me

6uy 2/22/2013 (IST) / Reply

hiiiiiiiiii

hiren 2/28/2013 (IST) / Reply

you have dose most languages to teaching

sravan kumar 4/15/2013 (IST) / Reply

nice

hemn 4/17/2013 (IST) / Reply

hello

newuser 4/24/2013 (IST) / Reply

Have you tried adding a password protected folder to this and does it work? It doesn't work for me. Thanks

geetha 4/30/2013 (IST) / Reply

how to create login form

geetha 4/30/2013 (IST) / Reply

how to create login form

sandeep 4/30/2013 (IST) / Reply

plz give me a how to create a login in mvc 4 help of sql server 2008

ytry 5/4/2013 (IST) / Reply

ryty

seetharaman 5/7/2013 (IST) / Reply

thanks to this site,those thing helps the user to know about the .net clealy.

maruthu 5/10/2013 (IST) / Reply

nice

sangeeth 5/21/2013 (IST) / Reply

dowamload

Dhinesh Kumar 5/24/2013 (IST) / Reply

nothing

Ritesh 5/26/2013 (IST) / Reply

Thankz a lot...nice and simple to understand

salil sharma 6/1/2013 (IST) / Reply

its too good..it helps me to make a registeration form and solve my id genrate problem

abae 6/10/2013 (IST) / Reply

hi

sadik 6/16/2013 (IST) / Reply

getting login error i.e invalid username and password

anand mathur 6/19/2013 (IST) / Reply

gud 1... post was an asset...

teja 7/10/2013 (IST) / Reply

dfjkfjkds

sokchea 7/24/2013 (IST) / Reply

I want to download source code asp.net with sql sever by using C#

vinoth 7/25/2013 (IST) / Reply

good

Narayan 7/25/2013 (IST) / Reply

need to download

ssssg 7/26/2013 (IST) / Reply

http://codemyne.net/articles/asp/CaptchaImage.axd?guid=983abe64-dbd0-43e1-be8a-d0f36251b977

Habtamu 7/27/2013 (IST) / Reply

ert

bob 7/28/2013 (IST) / Reply

Great. in finally and after registration just add con.Close()

vasudevan 7/29/2013 (IST) / Reply

need some help

gokulakrishnan 8/4/2013 (IST) / Reply

bgfbf

prasad 8/5/2013 (IST) / Reply

I AM NEW TO THIS TECHNOLOGY

ashish 8/10/2013 (IST) / Reply

dfar

rounak 8/14/2013 (IST) / Reply

learn

ram 8/17/2013 (IST) / Reply

i am learner about asp.net please help to download code

sothen 8/18/2013 (IST) / Reply

please help me

chinna 12/12/2013 (IST) / Reply

LJLKMLKM;LE;T;

iqab1 1/12/2014 (IST) / Reply

great work!!!!!!!

Divya 2/6/2014 (IST) / Reply

we need coding for our project.our project paper is"attribute based access to scalable media in cloud assisted content sharing network" we proposed to speed up the decryption operation so plz help us.

simbu 2/21/2014 (IST) / Reply

vb.net all code need

Vijay 7/18/2014 (IST) / Reply

Web Development

sandip 8/7/2014 (IST) / Reply

nice thats a osssam

Arjun 8/11/2014 (IST) / Reply

thanks

hi 10/8/2014 (IST) / Reply

nice code

archana 10/9/2014 (IST) / Reply

nice

INDRAREDDY 10/12/2014 (IST) / Reply

indrareddy

venu 10/19/2014 (IST) / Reply

Rggs dhhddhd

awais 11/27/2014 (IST) / Reply

help

asma 12/24/2014 (IST) / Reply

simple coding of registration

santhosh 1/13/2015 (IST) / Reply

this good coding parts i like it thank you santhosh M

nidhal 2/1/2015 (IST) / Reply

aaaaa

dabhi haresh k 2/12/2015 (IST) / Reply

ml,l;l

Nisha 3/7/2015 (IST) / Reply

thanks

Manoj Kumar 3/9/2015 (IST) / Reply

I want to read code for admin page in c#

venkat 3/25/2015 (IST) / Reply

help

Azhar 4/22/2015 (IST) / Reply

Hi All, I need yours help. I want to learn the .net language.what is the best and easiest way to learn the language. Could you please guide me.

prasant sharma 5/5/2015 (IST) / Reply

we want to singnup and login with same time .. in asp.net when we sign up then click and user login with the same time .. how can we do it

priya yadav 6/11/2015 (IST) / Reply

I need ur help I hving errors whn procedure is creating on checklogin plz help me

kiuo 6/16/2015 (IST) / Reply

dsafegteyg

ritesh 7/21/2015 (IST) / Reply

nice

vairamanikandan 7/22/2015 (IST) / Reply

how can i create email id in tamil font??

suraj 8/12/2015 (IST) / Reply

i am student of niit

shahid 9/19/2015 (IST) / Reply

superb code

sweta 9/27/2015 (IST) / Reply

hii

Rahul Kadam 11/1/2015 (IST) / Reply

Nic dud

tguhj 11/4/2015 (IST) / Reply

dsjteot6rdigfiktofr

gdfddgdg 11/18/2015 (IST) / Reply

hdnncjfvbvhffhfcvbvhfjbvbchnvcvbvb

Shivdas Suryawanshi 11/19/2015 (IST) / Reply

ssss

drashti 12/7/2015 (IST) / Reply

aaa

prasanna 12/14/2015 (IST) / Reply

abc

najmarasool 12/15/2015 (IST) / Reply

hallo

FAtma 2/7/2016 (IST) / Reply

thank

sasikala 2/11/2016 (IST) / Reply

How to create a simple registration form using .net in online using microsoft visual studio

Arshad 3/29/2016 (IST) / Reply

How i can register here to download code?

Davinder 4/1/2016 (IST) / Reply

where is NewGuid function to generate new guid?

jay 4/11/2016 (IST) / Reply

vfreh4yt