Monday, January 30, 2017

How to create Registration and Login Forn in asp.net using masterpage

Introduction

In this post I will explain how to implement simple login form using asp.net and I will explain how to Check Username and Password Exists in database using masterpage in asp.net .

There are Three steps create Login,Registration and Home Pages.

         1.Create Table in Database.
         2.Design .aspx pages.
         3. Write Code on .aspx.cs page.



Step 1. Create table in Database

Table Name: - useraccount














Query for creating a table












Step 2.1 Create/MasterPage .aspx page























Step 3.1 C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            Label1.Text = Session["username"].ToString();
            LinkButton1.Text = "Log out";
        }
        else
        {
            LinkButton1.Text = "Log in!";
        }
     
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (LinkButton1.Text == "Log in!")
        {
            Response.Redirect("Login.aspx");
        }
        else if (LinkButton1.Text == "Log out")
        {
            Session.Clear();
            Session.RemoveAll();
            Response.Redirect("Home.aspx");
         
        }
    }
}

Step 2.2 Create/Home .aspx page























Step 3.2 C# Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            Label1.Text = "Wel come";
           
        }
        else
        {
            Label1.Text = "Login First!";
        }
    }

}

Step 2.3 Create/Registration.aspx page






















Step 3.3 C# Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Registration : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Home.aspx");
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "insert into useraccount values(@name,@emailid,@password,@contactno)";
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@name",TextBox1.Text);
        cmd.Parameters.AddWithValue("@emailid", TextBox2.Text);
        cmd.Parameters.AddWithValue("@password", TextBox3.Text);
        cmd.Parameters.AddWithValue("@contactno", TextBox4.Text);
        cmd.ExecuteNonQuery();
        Session["username"] = TextBox2.Text;
        Response.Redirect("Home.aspx");
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Response.Redirect("Home.aspx");
    }

}

Step 2.4 Create/Login.aspx page






















Step 3.4 C# Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Login : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx");
    }
    protected void btnlogin_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from useraccount where emailid='"+TextBox1.Text+"' and password='"+TextBox2.Text+"'", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        int c = ds.Tables[0].Rows.Count;
        if (c > 0)
        {
            Session["username"] = TextBox1.Text;
            TextBox1.Text = "";
            TextBox2.Text = "";
            Response.Redirect("Home.aspx");
        }
   }
    protected void btncancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Home.aspx");
    }

}



Thank You...

No comments :

Post a Comment