Imports System.Data
Imports System.Data.SqlClient

Public Class addNews
    Inherits System.Web.UI.Page

    Protected WithEvents Conn As New SqlConnection(ConfigurationSettings.AppSettings("sConnection"))

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents NewsTable As System.Web.UI.HtmlControls.HtmlTable
    Protected WithEvents titleTB As System.Web.UI.WebControls.TextBox
    Protected WithEvents addNewsHeading As System.Web.UI.HtmlControls.HtmlTableCell
    Protected WithEvents sourceLinkTB As System.Web.UI.WebControls.TextBox
    Protected WithEvents dateTB As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator12 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator
    Protected WithEvents RequiredFieldValidator5 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents Requiredfieldvalidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents SaveBtn As System.Web.UI.WebControls.Button
    Protected WithEvents summaryTB As System.Web.UI.WebControls.TextBox
    Protected WithEvents Message As System.Web.UI.WebControls.Label
    Protected WithEvents DetailsTB As System.Web.UI.WebControls.TextBox
    Protected WithEvents NewsTypeDDL As System.Web.UI.WebControls.DropDownList

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim id As String = ""

        If Not Request.Params("id") Is Nothing Then
            id = Request.Params("id").ToString()
            addNewsHeading.InnerHtml = "Edit News"

            If Not Page.IsPostBack Then
                Message.Text = ""
                If (id <> "") Then
                    If (IsNumeric(id)) Then
                        If (CInt(id) > 0) Then
                            LoadNews(id)
                        End If
                    End If
                End If
            End If
        Else
            addNewsHeading.InnerHtml = "Add News"
        End If

        dateTB.Attributes.Add("onclick", "cal.select(document.getElementById('" & dateTB.ClientID & "'),'" & dateTB.ClientID & "','MM/dd/yyyy');")
        dateTB.Attributes.Add("style", "width:154px;")


    End Sub

    Private Sub LoadNews(ByVal id As String)
        Try
            Dim reader As SqlDataReader
            Dim cmd As New SqlCommand
            Dim query As String = ""

            Dim _title As String = titleTB.Text.Replace("'", "''")
            Dim _slink As String = sourceLinkTB.Text.Replace("'", "''")
            Dim _date As String = dateTB.Text.Replace("'", "''")
            Dim _summary As String = summaryTB.Text.Replace("'", "''")
            'Dim _details As String = FCKeditor1.Value.Replace("'", "''")
            Dim _details As String = DetailsTB.Text.Replace("'", "''")
            Dim _type As String

            query = "select * from as_News where ID = " & CType(id, Int32)
            If (query <> "") Then
                If Conn.State = ConnectionState.Closed Then
                    Conn.ConnectionString = ConfigurationSettings.AppSettings("sConnection")
                End If

                With cmd
                    .CommandText = query
                    .CommandType = CommandType.Text
                    If Conn.State = ConnectionState.Closed Then Conn.Open()
                    .Connection = Conn
                    reader = CType(.ExecuteReader(), SqlDataReader)
                End With

                If reader.HasRows Then
                    While reader.Read()
                        Dim date1 As DateTime = CType(reader("Date"), DateTime).ToShortDateString()
                        If (date1.Month < 10) Then
                            dateTB.Text = "0" & date1.Month
                        Else
                            dateTB.Text = date1.Month
                        End If

                        If (date1.Day < 10) Then
                            dateTB.Text = dateTB.Text & "/0" & date1.Day
                        Else
                            dateTB.Text = dateTB.Text & "/" & date1.Day
                        End If

                        dateTB.Text = dateTB.Text & "/" & date1.Year
                        titleTB.Text = reader("Title")
                        sourceLinkTB.Text = reader("Source")
                        summaryTB.Text = reader("Summary")
                        'FCKeditor1.Value = reader("Details")
                        DetailsTB.Text = reader("Details")
                        _type = reader("Type")
                    End While
                End If

                If Not _type = "" Then
                    If _type = "boosters" Then
                        NewsTypeDDL.SelectedIndex = 0
                    ElseIf _type = "home" Then
                        NewsTypeDDL.SelectedIndex = 1
                    ElseIf _type = "school" Then
                        NewsTypeDDL.SelectedIndex = 2
                    ElseIf _type = "church" Then
                        NewsTypeDDL.SelectedIndex = 3
                    ElseIf _type = "cevent" Then
                        NewsTypeDDL.SelectedIndex = 4
                    ElseIf _type = "sevent" Then
                        NewsTypeDDL.SelectedIndex = 5
                    Else
                        NewsTypeDDL.SelectedIndex = 6
                    End If
                End If
                If Conn.State = ConnectionState.Open Then Conn.Close()
            End If
        Catch ex As Exception
            If Conn.State = ConnectionState.Open Then Conn.Close()
            Response.Write(ex.Message)
        End Try
    End Sub
    Private Function GetBaseUrl() As String
        Dim context As HttpContext = HttpContext.Current
        Dim baseUrl As String = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd("/"c) + "/"c
        Return baseUrl
    End Function
    Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
        Try
            Dim cmd As New SqlCommand
            Dim query As String = ""
            Dim id As String = ""

            If Not Request.Params("id") Is Nothing Then
                id = Request.Params("id").ToString()
            End If

            Dim _title As String = titleTB.Text.Replace("'", "''")
            Dim _slink As String = sourceLinkTB.Text.Replace("'", "''")
            Dim _date As String = dateTB.Text.Replace("'", "''")
            Dim _summary As String = summaryTB.Text.Replace("'", "''")
            'Dim _details As String = FCKeditor1.Value.Replace("'", "''")
            Dim _details As String = DetailsTB.Text.Replace("'", "''")

            If (id = "") Then
                query = "insert into as_News values('" & _date & "','" & _title & "','" & _slink & "','" & _summary & "','" & _details & "',1,'" & NewsTypeDDL.SelectedValue & "')"
            Else
                query = "update as_News set Date='" & _date & "',Title='" & _title & "',Source='" & _slink & "',Summary='" & _summary & "',Details='" & _details & "',Type='" & NewsTypeDDL.SelectedValue & "' where ID = " & CType(id, Int32)
            End If

            If (query <> "") Then
                If Conn.State = ConnectionState.Closed Then
                    Conn.ConnectionString = ConfigurationSettings.AppSettings("sConnection")
                End If

                With cmd
                    .CommandText = query
                    .CommandType = CommandType.Text
                    If Conn.State = ConnectionState.Closed Then Conn.Open()
                    .Connection = Conn
                    .ExecuteNonQuery()
                End With
                If (id = "") Then
                    Message.Text = "News successfully saved"
                Else
                    Message.Text = "News successfully updated"
                End If
                Message.Attributes.Add("style", "text-align:center;color:green;")

                If Conn.State = ConnectionState.Open Then Conn.Close()
            End If
        Catch ex As Exception
            If Conn.State = ConnectionState.Open Then Conn.Close()
            Response.Write(ex.Message)
        End Try
    End Sub
End Class
