In this article i will give simple Login page. It may be very useful for beginners. if you have any queries feel free to ask me.
1. Create a login table
2. Insert User names and passwords into login table.
3. Design login page . Take two TextBoxes , one button and one result label.
4. Write code for Onclick Event in Login.cs file.
-- CREATE LOGINTABLE
CREATE TABLE LOGINTABLE
(
USERNAME VARCHAR(50),
PASSWORD VARCHAR(50)
)
-- INSERT INTO LOGINTABLE
INSERT INTO LOGINTABLE VALUES
(
'srinu.munagala33@gmail.com',
'07m11a1233'
)
SELECT * FROM LOGINTABLE
USERNAME | PASSWORD |
srinu.munagala33@gmail.com | 07m11a1233 |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="testLogin._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" >
<tr>
<td colspan="2">
<span> Login </span>
</td>
</tr>
<tr>
<td>
<span>User Name</span>
</td>
<td>
<asp:TextBox runat="server" ID="txtUserName" Width="180px" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<span>Password</span>
</td>
<td>
<asp:TextBox runat="server" ID="txtPassword" Width="180px" TextMode="Password" ></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button runat="server" ID="btnSubmit" Text="Login" OnClick="login_Submit" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Label runat="server" ID="lblResult" style=" color:Red ; font-size:20px;" ></asp:Label>
<span ></span>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" >
<tr>
<td colspan="2">
<span> Login </span>
</td>
</tr>
<tr>
<td>
<span>User Name</span>
</td>
<td>
<asp:TextBox runat="server" ID="txtUserName" Width="180px" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<span>Password</span>
</td>
<td>
<asp:TextBox runat="server" ID="txtPassword" Width="180px" TextMode="Password" ></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button runat="server" ID="btnSubmit" Text="Login" OnClick="login_Submit" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Label runat="server" ID="lblResult" style=" color:Red ; font-size:20px;" ></asp:Label>
<span ></span>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
.Cs
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;
namespace testLogin
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void login_Submit(object sender, EventArgs e)
{
SqlConnection SqlCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog =Test;Integrated Security=True");
try
{
SqlCon.Open();
SqlCommand SqlCom = new SqlCommand("select count(*) from LOGINTABLE where userName ='" + txtUserName.Text + "' and password ='" + txtPassword.Text + "'", SqlCon);
int i = Convert.ToInt32(SqlCom.ExecuteScalar());
if (i > 0)
{
lblResult.Text = "Login Success";
}
else
{
lblResult.Text = "Please enter valied username and password";
}
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
finally
{
SqlCon.Close();
}
}
}
}
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;
namespace testLogin
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void login_Submit(object sender, EventArgs e)
{
SqlConnection SqlCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog =Test;Integrated Security=True");
try
{
SqlCon.Open();
SqlCommand SqlCom = new SqlCommand("select count(*) from LOGINTABLE where userName ='" + txtUserName.Text + "' and password ='" + txtPassword.Text + "'", SqlCon);
int i = Convert.ToInt32(SqlCom.ExecuteScalar());
if (i > 0)
{
lblResult.Text = "Login Success";
}
else
{
lblResult.Text = "Please enter valied username and password";
}
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
finally
{
SqlCon.Close();
}
}
}
}
No comments:
Post a Comment