Dot Net WindowsForms Question:

Write a program to create login form?

Tweet Share WhatsApp

Answer:

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"] = "";
}
}
}

Download Dot Net WindowsForms PDF Read All 21 Dot Net WindowsForms Questions
Previous QuestionNext Question
Explain How to add resources during runtime?Explain something about crystal report in brief?