Thursday 27 December 2012

How to Login With SQL Server Database with VB(or C#) Language

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;

User Name: aspxcode , Password: 2008

   1. xml file or web.config


<?xml version="1.0"?>

  <configuration>

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

  </configuration>


2. how-to-login-with-sql-server.aspx (Design Page)
Login Coltrol ASP.NET
3. how-to-login-with-sql-server.aspx (Source Page)


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="how-to-login-with-sql-server.aspx.vb" Inherits="how_to_login_with_sql_server" %>

<!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>


4. how-to-login-with-sql-server.aspx.vb (Code Behind VB Language)

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class how_to_login_with_sql_server
    Inherits System.Web.UI.Page

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
        Dim username As String
        Dim pwd As String
        Dim pName As String

        username = Login1.UserName
        pwd = Login1.Password
        pName = ""
        Dim strConn As String
        strConn = WebConfigurationManager.ConnectionStrings("ConnectionASPX").ConnectionString

        Dim Conn As New SqlConnection(strConn)
        Conn.Open()


        Dim sqlUserName As String
        sqlUserName = "SELECT UserName,Password FROM UserName "
        sqlUserName &= " WHERE (UserName = @UserName"
        sqlUserName &= " AND Password = @Password)"

        Dim com As New SqlCommand(sqlUserName, Conn)
        com.Parameters.AddWithValue("@UserName", username)
        com.Parameters.AddWithValue("@Password", pwd)


        Dim CurrentName As String
        CurrentName = CStr(com.ExecuteScalar)

        If CurrentName <> "" Then
            Session("UserAuthentication") = username

            Response.Redirect("how-to-StartPage.aspx")
        Else
            Session("UserAuthentication") = ""
        End If

    End Sub
End Class

5. how-to-login-with-sql-server.aspx (View in Browser)
Login Coltrol ASP.NET
Login Coltrol ASP.NET

Convert to C#


refer from:http://www.aspxcode.net

No comments:

Post a Comment

If any doubt?then please comment in my post

How to reduce angular CLI build time

 Index: ----- I am using angular cli - 7 and I am going to tell how to reduce the build time as per my knowledge. Problem: -------- Now the ...