
Public Class DAL
    Private Shared Connection As System.Data.SqlClient.SqlConnection
   
    Public Shared Function initialize() As Boolean
        If Connection Is Nothing Then
            Dim Connection As New SqlClient.SqlConnection

            Dim strConnectionString As String = "Server=SALMA-H1STH3TGR;DataBase=CASFSU;Trusted_Connection=True"
            Connection.ConnectionString = strConnectionString
            Connection.Open()
        ElseIf Connection.State <> ConnectionState.Open Then

            Connection.ConnectionString = "Server=SALMA-H1STH3TGR;DataBase=CASFSU;Trusted_Connection=True"
            Connection.Open()
        End If
        Return True
    End Function
    Public Shared Function excuteSql(ByVal sql As String) As SqlClient.SqlDataReader
        initialize()
        Dim conn As New SqlClient.SqlCommand
        conn.CommandType = CommandType.Text
        conn.Connection = Connection
        conn.CommandText = sql
        Return conn.ExecuteReader
    End Function
    Public Shared Function dispose()
        Connection.Close()
        Connection.Dispose()
    End Function
End Class

---------------------------------------------------------------------------------------------

Imports System.Data.SqlClient
Imports System.Web.Security
Imports System.IO

Public Class SignOn
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents litNoAccount As System.Web.UI.WebControls.Literal
    Protected WithEvents txtName As System.Web.UI.WebControls.TextBox
    Protected WithEvents vldtxtName As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
    Protected WithEvents vldtxtPassword As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents butSignOn As System.Web.UI.WebControls.Button
    Protected WithEvents lblLogin As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub
    Private Function login(ByVal username As String, ByVal password As String) As Boolean
 	Dim dataReader As SqlDataReader 
        Dim sql As String = "SELECT Count(*)  FROM Users WHERE ((UserName= '" & txtName.Text & "') AND (Password='" & txtPassword.Text & "'))"
               
        dataReader = DAL.excuteSql(sql)
        If (dataReader.HasRows) Then
            dataReader.Read()
            If (CInt(dataReader(0)) > 0) Then
                System.Web.Security.FormsAuthentication.RedirectFromLoginPage(username, False)
                dataReader.Close()
                Return True
            End If
            dataReader.Close()
            Connection.Close()
        Else
            Return False
        End If
    End Function

    Private Function signOut() As Boolean
        System.Web.Security.FormsAuthentication.SignOut()
        
        Return True
    End Function

    Private Sub butSignOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSignOn.Click
        
        If (login(txtName.Text, txtPassword.Text)) Then
            lblLogin.Text = "Welcome " & Me.User.Identity.Name
        Else
            lblLogin.Text = "Please Enter valid credintials"
        End If



    End Sub
End Class
