using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Telerik.Web.UI;

public partial class Compose : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if (!Page.IsPostBack)
		{
			string messageID = Convert.ToString(Request["MessageID"]);
			if (!String.IsNullOrEmpty(messageID))
			{
				LoadEditor(messageID);
			}
		}
    }


    public void LoadEditor(string messageID)
    {
        string connstr = ConfigurationManager.ConnectionStrings["HelpDesk2ConnectionString"].ConnectionString;
        string text = String.Empty;
        SqlConnection dbCon = new SqlConnection(connstr);
        string strSelect = @"   SELECT  Text
                                    FROM Messages 
                                 WHERE messageID = @ID ";

        SqlCommand comm = new SqlCommand(strSelect, dbCon);
        comm.CommandType = CommandType.Text;

        SqlParameter id = new SqlParameter("@ID", messageID);
        comm.Parameters.Add(id);
        dbCon.Open();
        try
        {
            text = comm.ExecuteScalar().ToString();
        }
        finally
        {
            dbCon.Close();
        }
        RadEditor1.Content = text;

    }

	public void Button1_Click(object sender, EventArgs e)
	{

		string messageID = Convert.ToString(Request["messageID"]);


		if (!string.IsNullOrEmpty(messageID))
		{
			Update();
		}
		else
		{

			Insert();
		}


	}

	public void Update()
	{

		string connstr = ConfigurationManager.ConnectionStrings["HelpDesk2ConnectionString"].ConnectionString;


		SqlConnection dbCon = new SqlConnection(connstr);
		string strSelect = "  Update  Messages" + "   SET Text = @text  " + "WHERE  MessageID = @ID ";
		SqlCommand comm = new SqlCommand(strSelect, dbCon);
		comm.CommandType = CommandType.Text;
		SqlParameter text = new SqlParameter("@text", RadEditor1.Content);
		string messageID = Convert.ToString(Request["messageID"]);
		SqlParameter id = new SqlParameter("@ID", messageID);
		comm.Parameters.Add(text);
		comm.Parameters.Add(id);
		dbCon.Open();
		try
		{
			comm.ExecuteNonQuery();
		}
		catch (Exception Ex)
		{
			string x = Ex.Message;
		}
		finally
		{
			dbCon.Close();
			InjectScript.Text = "<script>OnClientSubmit()</" + "script>";
		}


	}

	public void Insert()
	{

		string connstr = ConfigurationManager.ConnectionStrings["HelpDesk2ConnectionString"].ConnectionString;


		SqlConnection dbCon = new SqlConnection(connstr);
		string strSelect = "  Insert INTO  Messages " + " VALUES ( @thread, @text, @date, @title , @sender , @receiver )  ";
		SqlCommand comm = new SqlCommand(strSelect, dbCon);
		comm.CommandType = CommandType.Text;
		SqlParameter thread = new SqlParameter("@thread", 1);
		comm.Parameters.Add(thread);
		SqlParameter text = new SqlParameter("@text", RadEditor1.Content);
		comm.Parameters.Add(text);
		SqlParameter date1 = new SqlParameter("@date", DateTime.Now);
		comm.Parameters.Add(date1);
		SqlParameter title = new SqlParameter("@title", RadEditor1.Content);
		comm.Parameters.Add(title);
		SqlParameter sender = new SqlParameter("@sender", 1);
		comm.Parameters.Add(sender);
		SqlParameter receiver = new SqlParameter("@receiver", 1);
		comm.Parameters.Add(receiver);
		dbCon.Open();
		try
		{
			comm.ExecuteNonQuery();
		}
		catch (Exception Ex)
		{
			string x = Ex.Message;
		}
		finally
		{
			dbCon.Close();
			InjectScript.Text = "<script>OnClientSubmit()</" + "script>";
		}
	}
	
}
