Imports System.Data

Partial Class UsersPermission
    Inherits System.Web.UI.Page

    Protected Function getUsertype(ByVal rowID As Integer) As String
        Dim strUsertype As String = ""
        Dim db As New FirstClass()
        Dim dt As New DataTable()

        dt = db.dbOut("SELECT UerID, UserTypeID FROM Users WHERE (UerID = '" & rowID & "')")

        strUsertype = dt.Rows(0)(1).ToString()
        Return strUsertype
    End Function

    Protected Sub grdFill()

        Dim db As New FirstClass()
        Dim dt As New DataTable()

        dt = db.dbOut("SELECT TOP 100 PERCENT dbo.Users.UerID AS UerID1, dbo.Users.UserName AS UserName1, dbo.Users.Name + N' ' + dbo.Users.Sname AS NameSname, " & vbCr & vbLf & " dbo.Users.EMail1 AS EmailOfUser, COUNT(dbo.tblNews.NewsID) AS CNtOfSentNews, dbo.Users.Banded AS banBit, " & vbCr & vbLf & " dbo.UserTypes.UserTypeDescript AS UtypeDecri FROM dbo.Users INNER JOIN dbo.UserTypes ON dbo.Users.UserTypeID = dbo.UserTypes.UserTypeID LEFT OUTER JOIN" & vbCr & vbLf & " dbo.tblNews ON dbo.Users.UserName = dbo.tblNews.UserName GROUP BY dbo.Users.UerID, dbo.Users.UserName, dbo.Users.Name + N' ' + dbo.Users.Sname, dbo.Users.EMail1, dbo.Users.Banded, " & vbCr & vbLf & " dbo.UserTypes.UserTypeDescript HAVING (dbo.Users.UserName <> N'admin') ORDER BY COUNT(dbo.tblNews.NewsID) DESC")

        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        grdFill()
    End Sub

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        grdFill()
    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        Dim db As New FirstClass()
        Dim usrEdtKey As Integer = CInt(GridView1.DataKeys(e.RowIndex).Value)
        db.cmd.Parameters.Add("@UerID", SqlDbType.Int).Value = usrEdtKey
        db.exeCommand("sp_Users_DeleteRow")
        'grdFill()
        Response.Redirect("UsersPermission.aspx?ID=" & DateTime.Now.Ticks.ToString())
    End Sub

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        grdFill()
        cmbFill()
        Dim db As New FirstClass()
        Dim dt As New DataTable()

        dt = db.dbOut("SELECT TOP 100 PERCENT UserTypeID, UserTypeDescript FROM UserTypes ORDER BY UserTypeID")

        Dim drop1 As New DropDownList()
        drop1 = DirectCast(GridView1.Rows(e.NewEditIndex).FindControl("DropDownList1"), DropDownList)
        DirectCast(GridView1.Rows(e.NewEditIndex).FindControl("DropDownList1"), DropDownList).DataSource = dt
        DirectCast(GridView1.Rows(e.NewEditIndex).FindControl("DropDownList1"), DropDownList).DataValueField = "UserTypeID"
        DirectCast(GridView1.Rows(e.NewEditIndex).FindControl("DropDownList1"), DropDownList).DataTextField = "UserTypeDescript"
        DirectCast(GridView1.Rows(e.NewEditIndex).FindControl("DropDownList1"), DropDownList).SelectedValue = getUsertype(Integer.Parse(DirectCast(GridView1.Rows(e.NewEditIndex).FindControl("Label3"), Label).Text))
        DirectCast(GridView1.Rows(e.NewEditIndex).FindControl("DropDownList1"), DropDownList).DataBind()
    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim db As New FirstClass()
        Dim blAllowshow As Integer = 0

        Dim drop1 As New DropDownList()
        Dim lbl As New Label()

        drop1 = DirectCast(GridView1.Rows(e.RowIndex).FindControl("DropDownList1"), DropDownList)
        lbl = DirectCast(GridView1.Rows(e.RowIndex).FindControl("Label3"), Label)
        Dim sssw As CheckBox = DirectCast(GridView1.Rows(e.RowIndex).Cells(6).Controls(0), CheckBox)
        Dim milPartner As String = DirectCast(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text

        If sssw.Checked Then
            blAllowshow = 1
        Else
            blAllowshow = 0
        End If

        db.cmd.Parameters.Add("@UerID", SqlDbType.Int).Value = Integer.Parse(lbl.Text.Trim())
        db.cmd.Parameters.Add("@UserTypeID", SqlDbType.Int).Value = Integer.Parse(drop1.SelectedValue.ToString())
        db.cmd.Parameters.Add("@Banded", SqlDbType.Bit).Value = blAllowshow
        db.cmd.Parameters.Add("@EMail1", SqlDbType.NVarChar).Value = milPartner.Trim()

        db.exeCommand("sp_Users_Update2")

        GridView1.EditIndex = -1
        'grdFill()
        Response.Redirect("UsersPermission.aspx?ID=" & DateTime.Now.Ticks.ToString())
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("UserName") IsNot Nothing Then
            If Integer.Parse(Session("UserTypeID").ToString()) = 1 Then
                If Not IsPostBack Then
                    grdFill()
                End If
            Else
                Response.Redirect("Default.aspx?ID=NotHavePermission" & DateTime.Now.Ticks.ToString())
            End If
        Else
            Response.Redirect("Default.aspx?ID=NotHavePermission" & DateTime.Now.Ticks.ToString())
        End If
    End Sub

    Protected Sub cmbFill()

    End Sub
End Class
