Monday 9 April 2012

How to check username and password for a login page Using Sql database in Asp.net

To implement this concept you need to follow the below steps : 

Step1 :
First you need to design a table in Sql Database to retrieve the records from database.

Step2:
Create a new Asp.net website in Visual Studio and write the following html code in the design part of the Default.aspx page.
 
Source code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            color: #3366FF;
            width: 507px;
            text-align: center;
            height: 60px;
        }
        .style2
        {
            width: 80px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 class="style1">
            <em>Login Status
    </em>
        </h2>
    </div>
    <div>
    <table bgcolor="#99CCFF" style="height: 125px; margin-left: 96px">
<tr>
<td class="style2">
Username:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" oncopy="return false" oncut="return false" onpaste="return false"/>
<asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" />
</td>
</tr>
<tr>
<td class="style2">
Password:
</td>
<td>
<asp:TextBox ID="txtPWD" runat="server" TextMode="Password" oncopy="return false" oncut="return false" onpaste="return false"/>
<asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password"/>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
    </div>
    </form>
</body>
</html>


Step3:
Now open the Default.aspx.cs page and write the following source code.

Default.aspx.cs 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 Default2 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=sridhar;Initial Catalog=Register;User ID=sa;Password=123");
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from person where Name =@Name and Password=@Password", con);
        cmd.Parameters.AddWithValue("@Name", txtUserName.Text);
        cmd.Parameters.AddWithValue("@Password", txtPWD.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Response.Redirect("Details.aspx");
        }
        else
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
        }
    }
}


Step4:
Now build the Solution and Debug it for the output.

OutPut :


No comments:

Post a Comment