﻿Imports System.Data.SqlClient
Public Class intro
    Inherits System.Web.UI.MasterPage
    Dim SqlConn As SqlConnection
    Dim SqlCommand As SqlCommand
    Dim SqlDataReader As SqlDataReader
    Dim SqlQuery 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()

    End Sub

  
    Protected Sub BtnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnLogin.Click

        If TxtUserName.Text = "" And TxtPassword.Text = "" Then
            LblLoginError.Text = "أدخل اسم المستخدم و كلمة السر"
            LblLoginError.Visible = True
            Exit Sub
        ElseIf TxtUserName.Text = "" Then
            LblLoginError.Text = "أدخل اسم المستخدم"
            LblLoginError.Visible = True
            Exit Sub
        ElseIf TxtPassword.Text = "" Then
            LblLoginError.Text = "أدخل كلمة السر"
            LblLoginError.Visible = True
            Exit Sub
        Else
            LblLoginError.Text = Nothing
            LblLoginError.Visible = False
        End If
        SqlConn = New SqlConnection(Session("Connection"))
        SqlConn.Open()
        SqlQuery = "SELECT UserName, Password, StudentName, StudentCode FROM StudentID WHERE (UserName ='" & TxtUserName.Text.ToString & "' and Password='" & TxtPassword.Text.ToString & "')"
        SqlCommand = New SqlCommand()
        SqlCommand.Connection = SqlConn
        SqlCommand.CommandText = SqlQuery
        'Dim result As Integer = SqlCommand.ExecuteNonQuery()
        SqlDataReader = SqlCommand.ExecuteReader()

        If SqlDataReader.HasRows Then
            SqlDataReader.Read()
            Session("StudentName") = SqlDataReader("StudentName")
            Session("UserName") = SqlDataReader("UserName")
            Session("Password") = SqlDataReader("Password")
            Session("StudentCode") = SqlDataReader("StudentCode")
        Else
            LblLoginError.Text = " اسم المستخدم أو كلمة السر خطأ، أعد المحاوله"
            TxtUserName.Text = ""
            TxtPassword.Text = ""
            LblLoginError.Visible = True
            Exit Sub
        End If
        SqlDataReader.Close()
        SqlDataReader = Nothing
        SqlCommand.Cancel()
        SqlCommand.Dispose()
        SqlConn.Close()
        Response.Redirect("mainpage.aspx")
    End Sub

    Private Sub TxtUserName_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtUserName.Init
        If TxtUserName.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 TxtUserName.TextChanged
        If TxtUserName.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 TxtPassword.TextChanged
        If TxtPassword.Text <> "" Then
            LblLoginError.Text = Nothing
            LblLoginError.Visible = False
        End If
    End Sub
  
End Class