write a program to create login form

Answer Posted / suman

The following example demonstrates how to use a Login
control to provide a user interface to log on to a Web site
with sql server database. VB : Imports System.Data ,
Imports System.Data.SqlClient and Imports
System.Web.Configuration , C# : using System.Data; , using
System.Data.SqlClient; and using System.Web.Configuration;
1. xml file or web.config
<?xml version="1.0"?>

<configuration>

<connectionStrings>
<add name="ConnectionASPX" connectionString="Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Northwi
nd.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>

</configuration>

2. how-to-login-with-sql-server-c.aspx (Design Page)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="how-
to-login-with-sql-server-c.aspx.cs"
Inherits="how_to_login_with_sql_server_c" %>

<!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>how to login with sql server database</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Login ID="Login1" runat="server"
BackColor="#FFFBD6" BorderColor="#FFDFAD" BorderPadding="4"
BorderStyle="Solid" BorderWidth="1px" Font-
Names="Verdana" Font-Size="0.8em"
ForeColor="#333333"
OnAuthenticate="Login1_Authenticate" TextLayout="TextOnTop"
Width="293px"
Height="172px">
<TitleTextStyle BackColor="#990000" Font-
Bold="True" Font-Size="0.9em"
ForeColor="White" />
<InstructionTextStyle Font-Italic="True"
ForeColor="Black" />
<TextBoxStyle Font-Size="0.8em" />
<LoginButtonStyle BackColor="White"
BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Verdana" Font-Size="0.8em"
ForeColor="#990000" />
</asp:Login>

</div>
</form>
</body>
</html>

3. how-to-login-with-sql-server-c.aspx.cs (Code Behind
C# Language)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class how_to_login_with_sql_server_c :
System.Web.UI.Page
{
protected void Login1_Authenticate(object sender,
AuthenticateEventArgs e)
{
string username = Login1.UserName;
string pwd = Login1.Password;

string strConn;
strConn = WebConfigurationManager.ConnectionStrings
["ConnectionASPX"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();

string sqlUserName;
sqlUserName = "SELECT UserName,Password FROM
UserName ";
sqlUserName += " WHERE (UserName ='" + username
+ "')";
sqlUserName += " AND (Password ='" + pwd + "')";
SqlCommand com = new SqlCommand(sqlUserName, Conn);

string CurrentName;
CurrentName = (string)com.ExecuteScalar();

if (CurrentName != null)
{
Session["UserAuthentication"] = username;
Session.Timeout = 1;
Response.Redirect("how-to-StartPage.aspx");
}
else
{
Session["UserAuthentication"] = "";
}
}
}

Is This Answer Correct ?    1 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to use the FindControl() function to preselect an item in my dropdownlist?

1707


how print PGL by XML

2623


Is windows an application software?

524


What is the full form of gac?

650


Where is system windows forms dll?

545






how print barcode

2113


Name the property which is used to lock a textbox to enter data?

535


To which namespace does the control class belong?

534


Which window allows viewing the resources of the servers, such as services, processes and the event log?

601


Which property of the errorprovider control automatically sets to the form to which?

554


Which method of the messagebox class is used to display a message in the message box?

566


Is typeform free?

520


What are windows based applications?

540


Explain how to split a column header in gridview using c#.net?

554


Explain how many number of events does the timer controls?

529