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 EditNews
    Inherits System.Web.UI.Page

    Protected Sub dropFill()
        Dim db As New FirstClass()
        Dim db2 As New FirstClass()

        Dim dt As New DataTable()
        Dim dt2 As New DataTable()

        dt = db.dbOut("SELECT TOP 100 PERCENT NewsGroupID, NewgGroupDescription FROM NewsGroups ORDER BY NewgGroupDescription")
        dt2 = db2.dbOut("SELECT NewsID, NewsGroupID, NewsTitle, newsBody FROM tblNews WHERE (NewsID = '" & Request.QueryString("NewsID").ToString() & "')")

        DropDownList1.DataSource = dt

        DropDownList1.DataTextField = "NewgGroupDescription"
        DropDownList1.DataValueField = "NewsGroupID"
        DropDownList1.SelectedValue = dt2.Rows(0)(1).ToString()
        TextBox1.Text = dt2.Rows(0)(2).ToString()
        TextBox2.Text = dt2.Rows(0)(3).ToString()

        DropDownList1.DataBind()
    End Sub

    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 db As New FirstClass()
        If FileUpload1.HasFile Then
            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

            db.cmd.Parameters.Add("@NewsID", SqlDbType.Int).Value = Integer.Parse(Request.QueryString("NewsID").ToString())
            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.ToString()

            db.exeCommand("sp_tblNews_Update")

            Response.Redirect("SelectToEditNews.aspx?ID=Edited=OK")
        Else
            db.cmd.Parameters.Add("@NewsID", SqlDbType.Int).Value = Integer.Parse(Request.QueryString("NewsID").ToString())
            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("@newsBody", SqlDbType.NText).Value = TextBox2.Text.ToString()

            db.exeCommand("sp_tblNews_Update2")
            Response.Redirect("SelectToEditNews.aspx?ID=Edited=OK")
        End If

    End Sub

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