Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class UserControl_suuplier_right_banner
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LinkButton1.NavigateUrl = Page.ResolveUrl("~/forgotpass.aspx")

        If Page.IsPostBack = False Then
            ScriptManager1.SetFocus(txtName)
            Session.Abandon()
        End If
    End Sub
    Protected Sub imgbtnlogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnlogin.Click
        Try

            Dim ds_login As DataSet
            Dim con As New SqlConnection()
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            Dim uname As String
            uname = txtName.Text
            If uname.Contains("'") Or uname.Contains("--") Or uname.Contains(";") Or uname.Contains(" ") Or uname.Contains("*") Or uname.Contains("where") Or uname.Contains("=") Then
                lblError.Visible = True
                lblError.Text = "Invalid UserName or Password."
                Exit Sub
            Else
                Dim ds As DataSet
                ds = AdminUserLog(uname, "Supplier")
                ds_login = SupLogin(uname.Trim, Encrypt(txtpwd.Text), "00000E6")
                If (ds.Tables(0).Rows.Count = 0) Then
                    If ds_login.Tables(0).Rows.Count <> 0 Then
                        Dim key As String = RemoveLiterals(NewPrimaryKeyUserControl(Me).ToString.Trim)
                        InsertAdminUserLog(key, txtName.Text, Request.UserHostAddress, 0, "Supplier", "AdminLogin", "Insert")
                        Session.Clear()
                        Session.LCID = 3081
                        Session("SupplierID") = Nothing
                        Session("SupplierID") = ds_login.Tables(0).Rows(0).Item("Supplier_Kid").ToString
                        Session("g_User") = ds_login.Tables(0).Rows(0).Item("Supplier_AdminUserId").ToString
                        Session("g_Company") = "00006Z4"
                        Session("g_FinYear") = "000010N"
                        lblError.Visible = False
                        Session("LogKid") = key
                        con.Close()
                        Response.Redirect("List_Product.aspx?" & Session.SessionID)

                    Else
                        lblError.Visible = True
                        lblError.Text = "Invalid UserName and Password !!"
                    End If
                Else
                    If (ds.Tables(0).Rows(0).Item(5).ToString = 0) Then
                        lblError.Text = "You are already logged in"
                        Exit Sub
                    End If
                    If ds_login.Tables(0).Rows.Count <> 0 Then
                        Dim key As String = RemoveLiterals(NewPrimaryKeyUserControl(Me).ToString.Trim)
                        InsertAdminUserLog(key, txtName.Text, Request.UserHostAddress, 0, "Supplier", "AdminLogin", "Insert")
                        Session.Clear()
                        Session.LCID = 3081
                        Session("SupplierID") = Nothing
                        Session("SupplierID") = Nothing
                        Session("SupplierID") = ds_login.Tables(0).Rows(0).Item("Supplier_Kid").ToString
                        Session("g_User") = ds_login.Tables(0).Rows(0).Item("Supplier_AdminUserId").ToString
                        Session("g_Company") = "00006Z4"
                        Session("g_FinYear") = "000010N"
                        lblError.Visible = False
                        Session("LogKid") = key 'ds.Tables(0).Rows(0).Item(0).ToString
                        Session("LogName") = ds.Tables(0).Rows(0).Item(1).ToString  'Response.Redirect("TestAdmin.aspx")
                        'Session("LogTime") = ds.Tables(0).Rows(0).Item(2).ToString
                        Session("LogTime") = Format$(Convert.ToDateTime(ds.Tables(0).Rows(0).Item(2).ToString), "dd-MMM-yyyy hh:mm:ss tt")
                        con.Close()
                        'InsertAdminUserLog( Session("LogKid"), txtName.Text, Request.UserHostAddress, 0, "AdminLogin", "Update")
                        'Response.Redirect("List_Product.aspx?" & Session.SessionID & "&logName=" & ds.Tables(0).Rows(0).Item(1).ToString & "&logTime=" & ds.Tables(0).Rows(0).Item(2).ToString)
                        Response.Redirect("List_Product.aspx?" & Session.SessionID)

                    Else
                        lblError.Visible = True
                        lblError.Text = "Invalid UserName and Password !!"
                    End If
                End If
            End If
        Catch ex As Exception
            'ex.Message.ToString()
        End Try
    End Sub
    Public Function SupLogin(ByVal name As String, ByVal password As String, ByVal usertype As String) As DataSet
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataSet
        cn.Open()

        Try
            objAdapter = New SqlDataAdapter("SupLogin_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@AdminUser_Name", SqlDbType.NVarChar, 200))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@AdminUser_Password", SqlDbType.NVarChar, 500))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@AdminUser_UserTypeId", SqlDbType.NVarChar, 10))
            objAdapter.SelectCommand.Parameters("@AdminUser_Name").Value = name
            objAdapter.SelectCommand.Parameters("@AdminUser_Password").Value = password
            objAdapter.SelectCommand.Parameters("@AdminUser_UserTypeId").Value = usertype
            ObjDS = New DataSet
            objAdapter.Fill(ObjDS)
            Return ObjDS

        Catch ex As Exception
            Throw ex
        Finally
            cn.Close()
            cn.Dispose()
        End Try

    End Function
End Class
