Imports System.Data.SqlClient
Partial Public Class _Default
    Inherits Functions

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.txtUsername.Focus()
    End Sub

    Protected Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        If Login() Then
            LogStats()
            Response.Redirect("/DashboardGroup.aspx")
        Else
            lblMessage.Text = "Invalid Username or Password"
        End If
    End Sub

    Protected Function Login() As Boolean
        Dim cmd As New SqlCommand
        Dim DR As SqlDataReader
        Dim DBConn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)


        cmd.CommandText = ("SELECT ID,Name,UserType FROM ReportServerUsers WHERE Username=@Username AND Password=@Password")
        cmd.Parameters.Add(New SqlParameter("@Username", SqlDbType.VarChar)).Value = txtUsername.Text
        cmd.Parameters.Add(New SqlParameter("@Password", SqlDbType.VarChar)).Value = txtPassword.Text
        cmd.Connection = DBConn
        DBConn.Open()
        DR = cmd.ExecuteReader()
        If DR.Read Then
            Session("user") = True
            Session("username") = txtUsername.Text
            Session("userid") = DR("ID")
            Session("name") = DR("Name")
            Session("usertype") = DR("UserType")

            Session.Timeout = 30
            Login = True
        Else
            Login = False
        End If
        DR.Close()

        'If Login Then
        '    cmd.CommandText = "SELECT TOP 1 * FROM Variables"
        '    DR = cmd.ExecuteReader
        '    If DR.Read Then
        '        Session("FixedOH") = CDbl(DR("FixedOH"))
        '        Session("FixedSell") = CDbl(DR("FixedSell"))
        '        Session("GA") = CDbl(DR("GA"))

        '    End If
        '    DR.Close()
        'End If
        DBConn.Close()

    End Function
End Class