Tuesday 6 January 2015

How to Create Simple Login Form With Use Of Validation in C# Using asp.net:



Hello Friends,
             Now I am Going to describe How to Create   Simple Login Form With Use Of  Validation  in C#  Using asp.net:
Step-1:
Create a login table and Insert  data  in this Table  like this:
Command of  sql  to  create above Table:
CREATE TABLE [dbo].[login] (
    [EmailId]  VARCHAR (50) NOT NULL,
    [Password] VARCHAR (50) NOT NULL
);

 
Step-2:
Design the Login Form Like This:
Write This code  in .aspx page   to design the above Form :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 17%;
            border: 1px solid #FF0000;
        }
        .auto-style4 {
        }
        .auto-style5 {
            width: 94%;
        }
        .auto-style6 {
            width: 60px;
            height: 26px;
        }
        .auto-style7 {
            width: 94%;
            height: 26px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1">
            <tr>
                <td colspan="4" style="color: #FF0000; font-size: medium; font-weight: bold">Login Form</td>
            </tr>
            <tr>
                <td class="auto-style6">EmailID</td>
                <td class="auto-style7">
                    <asp:TextBox ID="txtemailid" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style7">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtemailid" ErrorMessage="Please provide YourEmailid" ForeColor="Red"></asp:RequiredFieldValidator> </td>
                <td class="auto-style7">
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtemailid" ErrorMessage="Invalid  EmailId" ForeColor="#FF6600" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style4">Password</td>
                <td class="auto-style5">
                    <asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
                <td class="auto-style5">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtpassword" ErrorMessage="Please provide Your  Password" ForeColor="#FF6600"></asp:RequiredFieldValidator>
</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style4" colspan="2">
                    <asp:Button ID="btnlogin" runat="server" OnClick="btnlogin_Click" Text="Login" />
                </td>
                <td class="auto-style4">&nbsp;</td>
                <td class="auto-style4">&nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

Step-3:

Code of .aspx.cs Page:

Write Your Connection String in Web.config File:

<connectionStrings>
<add name="conn" connectionString="Data Source=xyz;Initial Catalog=x;Integrated Security=True"/>
  </connectionStrings>

Step-4:
Import  Following Namespace  Top of the .aspx.cs Page:

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

Write this code within public partial class:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);

Step-5:

Doubleclick on Login Button and Write This Code:

string sql = "select EmailId,Password  from login where EmailID=@EmailId and Password=@Password";
        SqlCommand cmd = new SqlCommand(sql,con);
        cmd.Parameters.AddWithValue("@EmailId", txtemailid.Text);
        cmd.Parameters.AddWithValue("@Password", txtpassword.Text);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Response.Redirect("~/Default2.aspx");
        }
        else
        {
            txtemailid.Text = "";
            string msg = "You Entered Invalid Username and Password";
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('"+msg+"')</script>");
        }




Thanks
Rabi

No comments:

Post a Comment