﻿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 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
        SqlConn = New SqlConnection(Session("Connection"))
        SqlConn.Open()
        SqlQuery = "SELECT UserName, Password, StudentName, StudentCode FROM StudentID WHERE (UserName ='" & TxtUserNameLogin.Text.ToString & "' and Password='" & TxtPasswordLogin.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 = " اسم المستخدم أو كلمة السر خطأ، أعد المحاوله"
            TxtUserNameLogin.Text = ""
            TxtPasswordLogin.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 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