Imports System
Imports System.Data
Imports System.Data.SqlClient

Partial Public Class Users
    Inherits System.Web.UI.Page
    Dim Connection As SqlConnection = New SqlConnection()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("user_id") = "") Then
            Response.Redirect("Login.aspx")
            Exit Sub
        End If
        Connection.ConnectionString = ConfigurationManager.ConnectionStrings("CalendarConnStr").ConnectionString
        If IsPostBack = False Then
            Bind_User_Record()
            gdv_user.FooterRow.Visible = False
        End If

    End Sub

    Public Sub Bind_User_Record()
        Me.lbl_alert_message.Text = ""
        Connection.ConnectionString = ConfigurationManager.ConnectionStrings("CalendarConnStr").ConnectionString
        Connection.Open()
        Dim Userhouse_Sql As String = "select * from user_master"
        Dim Userhouse_Adapter_no As New SqlDataAdapter(Userhouse_Sql, Connection)
        Dim Userhouse_DataSet_no As New DataSet()
        Userhouse_Adapter_no.Fill(Userhouse_DataSet_no, "user_master")
        If (Userhouse_DataSet_no.Tables(0).Rows.Count > 0) Then
            Me.gdv_user.DataSource = Userhouse_DataSet_no
            Me.gdv_user.DataBind()
        Else
            Userhouse_DataSet_no.Tables(0).Rows.Add(Userhouse_DataSet_no.Tables(0).NewRow())
            Me.gdv_user.DataSource = Userhouse_DataSet_no
            Me.gdv_user.DataBind()
            gdv_user.Rows(0).Cells.Clear()
        End If
        Connection.Close()
    End Sub

    Protected Sub gdv_user_RowEditing(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gdv_user.RowEditing
        If Session("user_type") = "a" Then
            Me.gdv_user.ShowFooter = False
            gdv_user.EditIndex = e.NewEditIndex
            Bind_User_Record()
        Else
            lbl_alert_message.Text = "You are not authorized !"
        End If
    End Sub

    Protected Sub gdv_user_RowUpdating(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gdv_user.RowUpdating

        Dim uid As String = CType(Me.gdv_user.Rows(e.RowIndex).Cells(0).Controls(1), Label).Text
        Dim userid As String = CType(Me.gdv_user.Rows(e.RowIndex).Cells(1).Controls(1), TextBox).Text
        Dim password As String = CType(Me.gdv_user.Rows(e.RowIndex).Cells(2).Controls(1), TextBox).Text
        Dim utype As String = CType(Me.gdv_user.Rows(e.RowIndex).Cells(3).Controls(1), DropDownList).SelectedValue
        Dim fname As String = CType(Me.gdv_user.Rows(e.RowIndex).Cells(4).Controls(1), TextBox).Text
        Dim lastname As String = CType(Me.gdv_user.Rows(e.RowIndex).Cells(5).Controls(1), TextBox).Text
        Dim email As String = CType(Me.gdv_user.Rows(e.RowIndex).Cells(6).Controls(1), TextBox).Text

        If userid.Length = 0 Then
            Me.lbl_alert_message.Text = "Please enter user ID !"
            Me.gdv_user.Rows(e.RowIndex).Cells(1).Controls(1).Focus()
            Exit Sub
        End If

        If password.Length = 0 Then
            Me.lbl_alert_message.Text = "Please enter user password ! "
            Me.gdv_user.Rows(e.RowIndex).Cells(2).Controls(1).Focus()
            Exit Sub
        End If

        Dim userupdCmd As SqlCommand
        userupdCmd = New SqlCommand("UPDATE user_master SET UserID = '" & userid & "',UPassword = '" & password & "' , UserFirstName = '" & fname & "' ,UserLastName = '" & lastname & "',UserEmailid = '" & email & "' ,user_type = '" & utype & "'  WHERE id = " & uid & "", Connection)
        Connection.ConnectionString = ConfigurationManager.ConnectionStrings("CalendarConnStr").ConnectionString
        Connection.Open()
        Try
            userupdCmd.ExecuteNonQuery()
        Catch ex As Exception
            lbl_alert_message.Text = "There is some problem in connecting the database. Please try again later. "
            Exit Sub
        End Try
        Connection.Close()
        Me.gdv_user.EditIndex = -1
        Me.gdv_user.ShowFooter = True
        Bind_User_Record()
    End Sub

    Protected Sub gdv_user_RowDeleting(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gdv_user.RowDeleting
        If Session("user_type") = "a" Then
            Me.gdv_user.EditIndex = -1
            Dim wid As String = CType(Me.gdv_user.Rows(e.RowIndex).Cells(0).Controls(1), Label).Text
            Dim userupdCmd As SqlCommand
            userupdCmd = New SqlCommand("delete from user_master  WHERE id = " & wid & "", Connection)
            Connection.ConnectionString = ConfigurationManager.ConnectionStrings("CalendarConnStr").ConnectionString
            Connection.Open()
            Try
                userupdCmd.ExecuteNonQuery()
            Catch ex As Exception
                lbl_alert_message.Text = "There is some problem in connecting the database. Please try again later. "
                Exit Sub
            End Try
            Connection.Close()
            Me.gdv_user.EditIndex = -1
            'Me.gdv_user.ShowFooter = True
            Bind_User_Record()
        Else
            lbl_alert_message.Text = "You are not authorized !"
        End If
    End Sub

    Protected Sub gdv_user_RowCancelingEdit(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gdv_user.RowCancelingEdit
        Me.gdv_user.EditIndex = -1
        Me.gdv_user.ShowFooter = True
        Bind_User_Record()
    End Sub

    Protected Sub gdv_user_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gdv_user.RowCommand
        Dim crid As Integer = Session("id")
        If e.CommandName = "Insert" Then
            Me.gdv_user.EditIndex = -1
            Dim newuserid As String = CType(gdv_user.FooterRow.FindControl("txt_newuserid"), TextBox).Text
            Dim newpassword As String = CType(gdv_user.FooterRow.FindControl("txt_newpassword"), TextBox).Text
            Dim newutype As String = CType(gdv_user.FooterRow.FindControl("ddlNew_utype"), DropDownList).SelectedValue
            'Dim accessoption As String = CType(gdv_user.FooterRow.FindControl("ddl_newaccessoption"), DropDownList).SelectedValue
            Dim newfname As String = CType(gdv_user.FooterRow.FindControl("txt_newfname"), TextBox).Text
            Dim newlastname As String = CType(gdv_user.FooterRow.FindControl("txt_newlastname"), TextBox).Text
            Dim newemail As String = CType(gdv_user.FooterRow.FindControl("txt_newemail"), TextBox).Text
            'Dim newcustomer As String = CType(gdv_user.FooterRow.FindControl("ddl_newcustomer"), DropDownList).SelectedValue
            'Dim newutype As String = CType(gdv_user.FooterRow.FindControl("ddlNew_utype"), DropDownList).SelectedValue
            'Dim newfax As String = CType(gdv_user.FooterRow.FindControl("txt_newfax"), TextBox).Text
            If newuserid.Length = 0 Then
                Me.lbl_alert_message.Text = "Please enter user ID !"
                Me.gdv_user.FooterRow.FindControl("txt_newuserid").Focus()
                Exit Sub
            End If
            If newpassword.Length = 0 Then
                Me.lbl_alert_message.Text = "Please enter password ! "
                Me.gdv_user.FooterRow.FindControl("txt_newpassword").Focus()
                Exit Sub
            End If
            'If newcustomer.Length = 0 Then
            '    Me.lbl_alert_message.Text = "Please enter customer ! "
            '    Me.gdv_user.FooterRow.FindControl("ddl_newcustomer").Focus()
            '    Exit Sub
            'End If
            Dim wareinsertCmd As SqlCommand
            wareinsertCmd = New SqlCommand("INSERT INTO user_master (UserID,uPassword , UserFirstName,UserLastName ,UserEmailid ,User_type,Createdby ) VALUES ( '" & newuserid & "', '" & newpassword & "',  '" & newfname & "', '" & newlastname & "', '" & newemail & "',  '" & newutype & "','" & crid & "')", Connection)
            Connection.ConnectionString = ConfigurationManager.ConnectionStrings("CalendarConnStr").ConnectionString
            Connection.Open()
            Try
                wareinsertCmd.ExecuteNonQuery()
            Catch ex As Exception
                lbl_alert_message.Text = "There is some problem in connecting the database. Please try again later. "
                Exit Sub
            End Try
            Connection.Close()
            Me.gdv_user.EditIndex = -1
            Me.gdv_user.ShowFooter = True
            Bind_User_Record()
            gdv_user.FooterRow.Visible = False
        End If
    End Sub

    Protected Sub btnEvent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEvent.Click
        Response.Redirect("Event.aspx")
    End Sub

    Protected Sub btnCategories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCategories.Click
        Response.Redirect("Category.aspx")
    End Sub

    Protected Sub btnClosedDates_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClosedDates.Click
        Response.Redirect("ClosedDates.aspx")
    End Sub

    Protected Sub btnUsers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUsers.Click
        Response.Redirect("Users.aspx")
    End Sub

    

    Protected Sub btnAddUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddUser.Click
        If Session("user_type") = "a" Then
            gdv_user.FooterRow.Visible = True
        Else
            lbl_alert_message.Text = "You are not authorized !"
        End If

    End Sub

    Protected Sub lnkLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkLogout.Click
        Session.Abandon()
        Response.Redirect("Login.aspx")
    End Sub
End Class