﻿Imports System.Data.OleDb
Public Class intro
    Inherits System.Web.UI.MasterPage
    Dim OleDBConn As OleDBConnection
    Dim OleDBCommand As OleDBCommand
    Dim OleDBDataReader As OleDBDataReader
    Dim OleDBQuery As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1))
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.Cache.SetNoStore()
        'Panel1.Visible = True
        'ModalPopupExtender1.Show()
        OleDBConn = New OleDbConnection(Session("Connection"))
        OleDBConn.Open()
        OleDBCommand = New OleDbCommand()
        OleDBCommand.Connection = OleDBConn
        OleDBQuery = "SELECT Count(VisitorCount.VisitorID) AS VisitorCount"
        OleDBQuery = OleDBQuery & " FROM VisitorCount GROUP BY VisitorCount.VisitDate HAVING (((VisitorCount.VisitDate)=#" & DateTime.Today.ToString() & "#));"
        OleDBCommand.CommandText = OleDBQuery
        OleDBDataReader = OleDBCommand.ExecuteReader()
        OleDBDataReader.Read()
        If OleDBDataReader.HasRows Then
            Session("VisitorCountToday") = OleDBDataReader("VisitorCount")
        End If
        OleDBDataReader.Close()
        OleDBQuery = "SELECT Count(VisitorCount.VisitorID) AS VisitorCount"
        OleDBQuery = OleDBQuery & " FROM VisitorCount;"
        OleDBCommand.CommandText = OleDBQuery
        OleDBDataReader = OleDBCommand.ExecuteReader()
        OleDBDataReader.Read()
        If OleDBDataReader.HasRows Then
            Session("VisitorCount") = OleDBDataReader("VisitorCount")
        End If
        OleDBDataReader.Close()
        OleDBCommand.Cancel()
        OleDBConn.Close()
    End Sub


    Protected Sub BtnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnLogin.Click

        If TxtUserNameLogin.Text = "" And TxtPasswordLogin.Text = "" Then
            LblLoginError.Text = "أدخل اسم المستخدم و كلمة السر"
            LblLoginError.Visible = True
            Exit Sub
        ElseIf TxtUserNameLogin.Text = "" Then
            LblLoginError.Text = "أدخل اسم المستخدم"
            LblLoginError.Visible = True
            Exit Sub
        ElseIf TxtPasswordLogin.Text = "" Then
            LblLoginError.Text = "أدخل كلمة السر"
            LblLoginError.Visible = True
            Exit Sub
        Else
            LblLoginError.Text = Nothing
            LblLoginError.Visible = False
        End If
        OleDBConn = New OleDBConnection(Session("Connection"))
        OleDBConn.Open()
        OleDBQuery = "SELECT UserName, Password, StudentName, StudentCode FROM StudentID WHERE (UserName ='" & TxtUserNameLogin.Text.ToString & "' and Password='" & TxtPasswordLogin.Text.ToString & "')"
        OleDBCommand = New OleDBCommand()
        OleDBCommand.Connection = OleDBConn
        OleDBCommand.CommandText = OleDBQuery
        'Dim result As Integer = OleDBCommand.ExecuteNonQuery()
        OleDBDataReader = OleDBCommand.ExecuteReader()

        If OleDBDataReader.HasRows Then
            OleDBDataReader.Read()
            Session("StudentName") = OleDBDataReader("StudentName")
            Session("UserName") = OleDBDataReader("UserName")
            Session("Password") = OleDBDataReader("Password")
            Session("StudentCode") = OleDBDataReader("StudentCode")
        Else
            LblLoginError.Text = " اسم المستخدم أو كلمة السر خطأ، أعد المحاوله"
            TxtUserNameLogin.Text = ""
            TxtPasswordLogin.Text = ""
            LblLoginError.Visible = True
            Exit Sub
        End If
        OleDBDataReader.Close()
        OleDBDataReader = Nothing
        OleDBCommand.Cancel()
        OleDBCommand.Dispose()
        OleDBConn.Close()
        Response.Redirect("mainpage.aspx")
    End Sub

    Private Sub TxtUserName_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtUserNameLogin.Init
        If TxtUserNameLogin.Text <> "" Then
            LblLoginError.Text = Nothing
            LblLoginError.Visible = False
        End If
    End Sub

    Private Sub TxtUserName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtUserNameLogin.TextChanged
        If TxtUserNameLogin.Text <> "" Then
            LblLoginError.Text = Nothing
            LblLoginError.Visible = False
        End If
    End Sub

    Private Sub TxtPassword_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtPasswordLogin.TextChanged
        If TxtPasswordLogin.Text <> "" Then
            LblLoginError.Text = Nothing
            LblLoginError.Visible = False
        End If
    End Sub

End Class