Monday, 26 December 2011

How to create Login Page in Asp.net with Connected Mode



How to create  Login Page in  Asp.net with Connected Mode……………………………

// create database test………………………

Create  database test
Use test

// create table LoginTable in database test………………………

create table LoginTable ( EmailId nvarchar(50) primary key,Password nvarchar(50),LoginTime nvarchar(50),Role nvarchar(50),Status nvarchar(50) ,SQues nvarchar(50),SAns nvarchar(50))

<%@ Page Title="Login Page" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table style="width: 100%">
    <tr>
        <td>
            &nbsp;</td>
        <td colspan="2">
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label5" runat="server" Font-Bold="True" Text="EmailId"></asp:Label>
        </td>
        <td colspan="2">
            <asp:TextBox ID="txtemailid" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                ControlToValidate="txtemailid" ErrorMessage="Please enter emailid"
                SetFocusOnError="True" ValidationGroup="g1" ForeColor="Red">*</asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                ControlToValidate="txtemailid" ErrorMessage="not valid format"
                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                ValidationGroup="g1" ForeColor="Red">*</asp:RegularExpressionValidator>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label7" runat="server" Font-Bold="True" Text="Password"></asp:Label>
        </td>
        <td colspan="2">
            <asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                ControlToValidate="txtpassword" ErrorMessage="Please enter password"
                ValidationGroup="g1" ForeColor="Red">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td style="height: 27px">
            &nbsp;</td>
        <td style="height: 27px">
            <asp:Button ID="Button2" runat="server" Height="25px" onclick="Button1_Click"
                Text="Login" ValidationGroup="g1" />
        </td>
        <td style="height: 27px">
            <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/Regitration.aspx">Sign Up</asp:HyperLink>
        </td>
    </tr>
    <tr>
        <td colspan="3" style="height: 27px">
            <center>
                <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Forget Password</asp:LinkButton>
            </center>
        </td>
    </tr>
    <tr>
        <td colspan="3" style="height: 27px" align="center">
                <asp:Label ID="Label17" runat="server" Visible="False" ForeColor="Red"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
        <td colspan="2">
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" />
        </td>
    </tr>
</table>
</asp:Content>

// this  code  for  web configuration file …………………

<configuration>
          <connectionStrings>
         <add name="kush" connectionString="Data Source=KUSH-PC\KUSH;Initial Catalog=SocialNet;Integrated Security=True"/>
                    
</connectionStrings>

 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;
using System.IO;
using System.Web.Security;

public partial class Login : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;

    protected void Page_Load(object sender, EventArgs e)
    {
         
con = new SqlConnection(ConfigurationManager.ConnectionStrings["kush"].ConnectionString);
     
    }
   protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
     cmd = new SqlCommand("select EmailId,Password,Status from LoginTable where EmailId=@EmailId and Password= @Password", con);

            cmd.Parameters.AddWithValue("@EmailId", txtemailid.Text);
            cmd.Parameters.AddWithValue("@Password", txtpassword.Text);

            con.Open();
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                if (dr["Status"].ToString() == "True")
                {
                    Session["email"] = dr["EmailId"];
                    FormsAuthentication.RedirectFromLoginPage("a", false);
                    Response.Redirect("UserHome.aspx");

                }
                else
                {
                    Label17.Visible = true;
                    Label17.Text = "Your Account is not active";

                }
            }
            else
            {
                Label17.Visible = true;
                Label17.Text = "EmailId  and Password is  not match Please try again...........";

            }
        }
        catch (Exception ex)
        {
            Label17.Visible = true;
            Label17.Text = ex.Message;

        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session["email"] = txtemailid.Text;
        Response.Redirect("ForgetPassword.aspx");
    }
}

0 comments:

Post a Comment