Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Partial Class AddNewsGroup
    Inherits System.Web.UI.Page

    Protected Sub grdFill()
        Dim db As New FirstClass()
        Dim dt As New DataTable()

        dt = db.dbOut("SELECT TOP 100 PERCENT NewsGroups.NewsGroupID AS nwsID, NewsGroups.NewgGroupDescription AS nwsGroup, " & vbCr & vbLf & " COUNT(tblNews.NewsGroupID) AS cntArts FROM NewsGroups LEFT OUTER JOIN tblNews ON NewsGroups.NewsGroupID = tblNews.NewsGroupID" & vbCr & vbLf & " GROUP BY NewsGroups.NewsGroupID, NewsGroups.NewgGroupDescription ORDER BY NewsGroups.NewsGroupID DESC")

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

    Protected Sub dbFill()
        Dim db As New FirstClass()
        db.cmd.Parameters.Add("@NewgGroupDescription", SqlDbType.NVarChar).Value = TextBox1.Text.Trim()

        db.exeCommand("sp_NewsGroups_Insert")

        'grdFill()
        TextBox1.Text = ""
        Response.Redirect("AddNewsGroup.aspx?ID=EditOK" & DateTime.Now.Ticks.ToString())
    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()
        Response.Redirect("AddNewsGroup.aspx?ID=EditOK" & DateTime.Now.Ticks.ToString())
    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()
        Response.Redirect("AddNewsGroup.aspx?ID=EditOK" & DateTime.Now.Ticks.ToString())
    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 usrDelKey As Integer = CInt(GridView1.DataKeys(e.RowIndex).Value)
        db.cmd.Parameters.Add("@NewsGroupID", SqlDbType.Int).Value = usrDelKey
        db.exeCommand("sp_NewsGroups_DeleteRow")
        'grdFill()
        Response.Redirect("AddNewsGroup.aspx?ID=EditOK" & 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()
        'Response.Redirect("AddNewsGroup.aspx?ID=EditOK" & DateTime.Now.Ticks.ToString())
    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 lbl As New Label()
        Dim txtNewsGroup As String = DirectCast(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
        'Response.Write(txtNewsGroup)
        lbl = DirectCast(GridView1.Rows(e.RowIndex).FindControl("Label1"), Label)

        db.cmd.Parameters.Add("@NewsGroupID", SqlDbType.Int).Value = Integer.Parse(lbl.Text.Trim())
        db.cmd.Parameters.Add("@NewgGroupDescription", SqlDbType.NVarChar).Value = txtNewsGroup

        db.exeCommand("sp_NewsGroups_Update")
        GridView1.EditIndex = -1
        Response.Redirect("AddNewsGroup.aspx?ID=EditOK" & DateTime.Now.Ticks.ToString())
        'grdFill()
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label3.Visible = False
        Dim db As New FirstClass()
        Dim dt As New DataTable()

        dt = db.dbOut("SELECT TOP 1 NewgGroupDescription FROM NewsGroups WHERE (NewgGroupDescription = N'" & TextBox1.Text.Trim() & "')")
        If dt.Rows.Count <= 0 Then
            dbFill()
        Else
            Label3.Visible = True
        End If
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""
    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

End Class
