using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Web.Configuration; using System.Text; // Encoding namespace BasicAjaxCSharp { public partial class CheckForMessages : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection connection; SqlCommand command; SqlDataReader reader; string recipient = ""; // Set the content type Response.ContentType = "text/plain"; Response.ContentEncoding = Encoding.UTF8; // Prevent page from being cached when we are polling Response.Expires = -1; if (Request.Form["recipientname"] != null && Request.Form["recipientname"] != "") { recipient = Request.Form["recipientname"]; try { connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString); command = new SqlCommand("SELECT * FROM StudentMessage WHERE Recipient=@Recipient ORDER BY DateReceived DESC", connection); command.Parameters.AddWithValue("@Recipient", recipient); using (connection) { connection.Open(); reader = command.ExecuteReader(); if (reader.HasRows) { reader.Read(); Response.Write((DateTime)reader["DateReceived"] + "
"); // Should use StringBuilder Response.Write("||"); Response.Write(reader["Sender"] + "
"); Response.Write("||"); Response.Write(reader["Text"] + "
"); } else { Response.Write("-1||No matching row found"); } connection.Close(); } } catch { Response.Write("-1||An error occurred"); } } else { Response.Write("-1||No key provided for search"); } } } }