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?

1699


What are windows applications?

493


What are the different cloud platforms?

538


What is the use of form?

529


Name the template that is used to create a user-defined component.

515






Which template creates a .net compact framework 2.0 forms application for pocket pc 2003 and later?

559


What is form description?

514


What is form record?

536


Explain about crystal report in brief?

552


What are windows based applications examples?

537


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

548


Which command prompt utility is used to convert a resource file from the text format into the binary format?

605


Explain how save rerecord in the database?

582


Is windows an application software?

519


Is form action required?

513