Imports System
Imports System.Data
Imports System.Data.SqlClient
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
Imports System.IO
Imports Telerik.Web.UI

Partial Public Class ComposeVB
	Inherits System.Web.UI.Page
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
		If Not Page.IsPostBack Then
			Dim messageID As String = Convert.ToString(Request("MessageID"))
			If Not [String].IsNullOrEmpty(messageID) Then
				LoadEditor(messageID)
			End If
		End If
	End Sub


	Public Sub LoadEditor(ByVal messageID As String)
		Dim connstr As String = ConfigurationManager.ConnectionStrings("HelpDesk2ConnectionString").ConnectionString
		Dim text As String = [String].Empty
		Dim dbCon As New SqlConnection(connstr)
		Dim strSelect As String = "  SELECT  Text" & _
										 "   FROM Messages " & _
										 "WHERE  MessageID = @ID "

		Dim comm As New SqlCommand(strSelect, dbCon)
		comm.CommandType = CommandType.Text

		Dim id As New SqlParameter("@ID", messageID)
		comm.Parameters.Add(id)
		dbCon.Open()
		Try
			text = comm.ExecuteScalar().ToString()
		Finally
			dbCon.Close()
		End Try
        RadEditor1.Content = text

	End Sub

	Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

		Dim messageID As String = Convert.ToString(Request("messageID"))


		If Not String.IsNullOrEmpty(messageID) Then
			Update()
		Else

			Insert()
		End If


	End Sub

	Public Sub Update()

		Dim connstr As String = ConfigurationManager.ConnectionStrings("HelpDesk2ConnectionString").ConnectionString


		Dim dbCon As New SqlConnection(connstr)
		Dim strSelect As String = "  Update  Messages" & _
									   "   SET Text = @text  " & _
									   "WHERE  MessageID = @ID "
		Dim comm As New SqlCommand(strSelect, dbCon)
		comm.CommandType = CommandType.Text
		Dim text As New SqlParameter("@text", RadEditor1.Content)
		Dim messageID As String = Convert.ToString(Request("messageID"))
		Dim id As New SqlParameter("@ID", messageID)
		comm.Parameters.Add(text)
		comm.Parameters.Add(id)
		dbCon.Open()
		Try
			comm.ExecuteNonQuery()
		Catch Ex As Exception
			Dim x As String = Ex.Message
		Finally
			dbCon.Close()
			InjectScript.Text = "<script>OnClientSubmit()</" + "script>"
		End Try


	End Sub

	Public Sub Insert()

		Dim connstr As String = ConfigurationManager.ConnectionStrings("HelpDesk2ConnectionString").ConnectionString


		Dim dbCon As New SqlConnection(connstr)
		Dim strSelect As String = "  Insert INTO  Messages " & _
									  " VALUES ( @thread, @text, @date, @title , @sender , @receiver )  "
		Dim comm As New SqlCommand(strSelect, dbCon)
		comm.CommandType = CommandType.Text
		Dim thread As New SqlParameter("@thread", 1)
		comm.Parameters.Add(thread)
		Dim text As New SqlParameter("@text", RadEditor1.Content)
		comm.Parameters.Add(text)
		Dim date1 As New SqlParameter("@date", DateTime.Now)
		comm.Parameters.Add(date1)
		Dim title As New SqlParameter("@title", RadEditor1.Content)
		comm.Parameters.Add(title)
		Dim sender As New SqlParameter("@sender", 1)
		comm.Parameters.Add(sender)
		Dim receiver As New SqlParameter("@receiver", 1)
		comm.Parameters.Add(receiver)
		dbCon.Open()
		Try
			comm.ExecuteNonQuery()
		Catch Ex As Exception
			Dim x As String = Ex.Message
		Finally
			dbCon.Close()
			InjectScript.Text = "<script>OnClientSubmit()</" + "script>"
		End Try
	End Sub

End Class


