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 PostRequest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection connection;
SqlCommand command;
SqlDataReader reader;
int studentId = 0;
// Set the content type
Response.ContentType = "text/plain";
Response.ContentEncoding = Encoding.UTF8;
if (Request.Form["studentid"] != null
&& Request.Form["studentid"] != "")
{
studentId = int.Parse(Request.Form["studentid"]);
try
{
connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString);
command = new SqlCommand("SELECT * FROM Student WHERE Id=@Id", connection);
command.Parameters.AddWithValue("@Id", studentId);
using (connection)
{
connection.Open();
reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader["Name"] + "
");
Response.Write((int)reader["Age"] + "
");
Response.Write(reader["Course"] + "
");
}
}
else
{
Response.Write("No matching row found");
}
connection.Close();
}
}
catch
{
Response.Write("An error occurred");
}
}
else
{
Response.Write("No ID provided for search");
}
}
}
}