Imports System.Data

Partial Class KhabarMaster
    Inherits System.Web.UI.MasterPage

    Protected Sub grdFill()
        Dim db As New FirstClass()
        Dim db1 As New FirstClass()

        Dim dt As New DataTable()
        Dim dt1 As New DataTable()

        dt = db.dbOut("SELECT TOP 100 PERCENT NewsGroupID, NewgGroupDescription FROM NewsGroups ORDER BY NewgGroupDescription")
        If Session("UserName") Is Nothing Then
            dt1 = db1.dbOut("SELECT TOP 100 PERCENT linkID, MainLinkDescription, MainLinkLink, SortingID, AllowShowBita FROM MainLinks WHERE (AllowShowBita = 0) OR (AllowShowBita = 2) ORDER BY SortingID")
        Else
            If Integer.Parse(Session("UserTypeID").ToString()) = 1 Then
                dt1 = db1.dbOut("SELECT TOP 100 PERCENT linkID, MainLinkDescription, MainLinkLink, SortingID, AllowShowBita FROM MainLinks WHERE (AllowShowBita = 0) OR (AllowShowBita = 1) OR (AllowShowBita = 3) OR (AllowShowBita = 4) ORDER BY SortingID")
            ElseIf Integer.Parse(Session("UserTypeID").ToString()) = 2 Then
                dt1 = db1.dbOut("SELECT TOP 100 PERCENT linkID, MainLinkDescription, MainLinkLink, SortingID, AllowShowBita FROM MainLinks WHERE (AllowShowBita = 0) OR (AllowShowBita = 1) OR (AllowShowBita = 4) ORDER BY SortingID")
            Else
                dt1 = db1.dbOut("SELECT TOP 100 PERCENT linkID, MainLinkDescription, MainLinkLink, SortingID, AllowShowBita FROM MainLinks WHERE (AllowShowBita = 0) OR (AllowShowBita = 1) ORDER BY SortingID")
            End If
        End If

        GridView2.DataSource = dt
        GridView1.DataSource = dt1

        GridView2.DataBind()
        GridView1.DataBind()
        fillMysiteinfo()
        archiveAdd()
    End Sub

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

        dt = db.dbOut("SELECT TOP 15 ArchivedDate FROM tblNews WHERE (ArchivedBit = 1) GROUP BY ArchivedDate HAVING (ArchivedDate IS NOT NULL) ORDER BY ArchivedDate DESC")

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

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

        dt = db.dbOut("SELECT DERIVEDTBL_3.VisitID AS BazdidHa, DERIVEDTBL.Expr1 AS khabariSiteUsers, DERIVEDTBL_1.Expr2 AS AddedNews, " & vbCr & vbLf & " DERIVEDTBL_2.Expr1 AS HasGroupsIn FROM (SELECT COUNT(UserName) AS Expr1 FROM Users" & vbCr & vbLf & " GROUP BY Banded HAVING (Banded = 0)) DERIVEDTBL CROSS JOIN (SELECT COUNT(NewsID) AS Expr2" & vbCr & vbLf & " FROM tblNews) DERIVEDTBL_1 CROSS JOIN (SELECT COUNT(NewsGroupID) AS Expr1 FROM NewsGroups) DERIVEDTBL_2 CROSS JOIN" & vbCr & vbLf & " (SELECT VisitID FROM VisitCounts) DERIVEDTBL_3")

        Label2.Text = dt.Rows(0)(0).ToString()
        Label3.Text = dt.Rows(0)(1).ToString()
        Label4.Text = dt.Rows(0)(2).ToString()
        Label5.Text = dt.Rows(0)(3).ToString()
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        grdFill()
        If Session("UserName") IsNot Nothing Then
            Label1.Visible = True
            Panel1.Visible = False
            Label1.Text = (Session("NameSname").ToString() & "،") + Session("UserTypeDescript").ToString() & " خوش آمدید "
        End If
    End Sub

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

        dt = db.dbOut(("SELECT Users.UerID, Users.UserName, Users.Password, Users.Name + N' ' + Users.Sname AS NameSname, " & vbCr & vbLf & " UserTypes.UserTypeDescript, Users.Banded, Users.UserTypeID FROM Users INNER JOIN UserTypes ON Users.UserTypeID = UserTypes.UserTypeID WHERE (Users.Banded = 0) AND (Users.UserName = N'" & TextBox1.Text.Trim() & "') AND (Users.Password = N'") + TextBox2.Text.Trim() & "')")

        If dt.Rows.Count <= 0 Then
            Label1.Visible = True
            Label1.Text = "نام کاربری موجود نیست"
        Else
            Label1.Visible = True
            Panel1.Visible = False
            Session.Add("UserName", dt.Rows(0)(1).ToString())
            Session.Add("NameSname", dt.Rows(0)(3).ToString())
            Session.Add("UserTypeDescript", dt.Rows(0)(4).ToString())
            Session.Add("UserTypeID", dt.Rows(0)(6).ToString())
            Label1.Text = (Session("NameSname").ToString() & "،") + Session("UserTypeDescript").ToString() & " خوش آمدید "
            Response.Redirect("Default.aspx?ID=" & DateTime.Now.Ticks.ToString())
        End If
    End Sub
End Class

