Imports System.Data

Partial Class AddNews
    Inherits System.Web.UI.Page

    Private Function GetNameForFile() As String
        Dim db As New FirstClass()
        Dim DateNow As String = db.dtShamsi()
        DateNow = DateNow.Replace("/", "")
        DateNow += DateTime.Now.Hour.ToString().Trim() + DateTime.Now.Minute.ToString().Trim() + DateTime.Now.Second.ToString().Trim()
        Return DateNow.Trim()
    End Function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ImgExtention As String = ""
        Dim FileNme As String = ""
        If FileUpload1.HasFile Then
            ImgExtention = System.IO.Path.GetExtension(FileUpload1.FileName)
        End If
        If ImgExtention <> "" Then
            FileNme = GetNameForFile()
        End If
        If FileUpload1.HasFile Then
            Dim savePath As String = (Server.MapPath("UploadedUserFiles") & "\") + FileNme + ImgExtention
            FileUpload1.SaveAs(savePath)
        End If

        Dim db As New FirstClass()
        db.cmd.Parameters.Add("@NewsTitle", SqlDbType.NVarChar).Value = TextBox1.Text.Trim()
        db.cmd.Parameters.Add("@NewsGroupID", SqlDbType.Int).Value = DropDownList1.SelectedValue
        db.cmd.Parameters.Add("@NewsPic", SqlDbType.NVarChar).Value = (("UploadedUserFiles") & "\") + FileNme + ImgExtention
        db.cmd.Parameters.Add("@newsBody", SqlDbType.NText).Value = TextBox2.Text.Trim()
        db.cmd.Parameters.Add("@DateOfAdding", SqlDbType.NVarChar).Value = db.dtShamsi()
        db.cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = Session("UserName").ToString()
        db.cmd.Parameters.Add("@ArchivedBit", SqlDbType.Bit).Value = 0
        If Integer.Parse(Session("UserTypeID").ToString()) = 3 Then
            db.cmd.Parameters.Add("@ShowPermiss", SqlDbType.Bit).Value = 0
        Else
            db.cmd.Parameters.Add("@ShowPermiss", SqlDbType.Bit).Value = 1
        End If

        db.exeCommand("sp_tblNews_Insert")
        Response.Redirect("Default.aspx?" & DateTime.Now.Ticks.ToString())
        'txtClear()

    End Sub

    Protected Sub txtClear()
        TextBox1.Text = ""
        TextBox2.Text = ""
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        txtClear()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("UserName") Is Nothing Then
            Response.Redirect("Default.aspx?ID=" & DateTime.Now.Ticks.ToString())
        End If

        If Not IsPostBack Then
            Dim db As New FirstClass()
            Dim dt As New DataTable()

            dt = db.dbOut("SELECT TOP 100 PERCENT NewsGroupID, NewgGroupDescription FROM NewsGroups ORDER BY NewgGroupDescription")

            DropDownList1.DataSource = dt

            DropDownList1.DataTextField = "NewgGroupDescription"
            DropDownList1.DataValueField = "NewsGroupID"

            DropDownList1.DataBind()
        End If
    End Sub
End Class
