jqueryajax using json

Posted by Klbaiju under jQuery on 5/12/2011 | Points: 10 | Views : 1678 | Status : [Member] | Replies : 3
Hi below code is for displaying data from database using jqueryajax
jqueryajax


<script type="text/javascript" language="Javascript" src="Script/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#Customers").change(function() {
// alert($('#Customers').val());
var name = $('#Customers').val();
// alert(name);
var ID = 'CustomerID=' + name;
$.ajax({
type: "GET",
contentType: "text/html; charset=utf-8",
url: "jQueryAjaxData.aspx",
data: ID,
success: function(data) {
$("#CustomerDetails").html(data);
}
});
return false;
});
});
</script>




Responses

Posted by: Klbaiju on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
jqueryajaxdata.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Response.ClearContent();
Response.Clear();

string CustomerID = Request.QueryString["CustomerID"];
if (Request.QueryString["CustomerID"] != null)
{
// GetCustomer(CustomerID);
Response.Write(GetCustomer(CustomerID));
// Response.Write("No data get");
}
else
{

Response.Write("No data get");
}
}

public string GetCustomer(string CustomerID)
{
string response = "<p>No customer selected</p>";
string connect = "Server=baiju;Database=Northwind;Trusted_Connection=True";
string query = "SELECT CompanyName, Address, City, Region, PostalCode,Country, Phone, Fax FROM Customers where customerid=@CustomerID ";
if (CustomerID != null && CustomerID.Length == 5)
{
StringBuilder sb = new StringBuilder();
using (SqlConnection conn = new SqlConnection(connect))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("CustomerID", CustomerID);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{

sb.Append("<p>");
sb.Append("<strong>" + "Company " + "</strong>");
sb.Append("<strong>" + "Address " + "</strong>");
sb.Append("<strong>" + "City " + "</strong>");
sb.Append("<strong>" + "Region " + "</strong>");
sb.Append("<strong>" + "PostalCode " + "</strong>");
sb.Append("<strong>" + "Country " + "</strong>");
sb.Append("<strong>" + "Phone " + "</strong>");

sb.Append("<strong>" + "Fax " + "</strong><br/>");

while (rdr.Read())
{
sb.Append(rdrCompanyName"].ToString());

sb.Append(rdr["Address"].ToString());
sb.Append(rdr["City"].ToString());
sb.Append(rdr["Region"].ToString());
sb.Append(rdr["PostalCode"].ToString());
sb.Append(rdr["Country"].ToString());
sb.Append(rdr["Phone"].ToString());
sb.Append(rdr["Fax"].ToString() + "<br/>");
response = sb.ToString();
}
}
}
}
}
return response;
}



Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
my problem is i want to display the records in table like this
if (rdr.HasRows)
{
Response.Write("<table border='1'><tr style='background-color: #008000'; align='center'><td background-color:green><strong>Company</strong></td>");
Response.Write("<td><strong>Address</strong> </td>");
Response.Write("<td><strong>City</strong> </td>");
Response.Write("<td><strong>Region</strong> </td>");
Response.Write("<td><strong>PostalCode</strong> </td>");
Response.Write("<td><strong>Country</strong> </td>");
Response.Write("<td><strong>Phone</strong> </td>");
Response.Write("<td><strong>Fax</strong> </td></tr>");

}
while (rdr.Read())
{
Response.Write("<tr><td> " + (rdr["CompanyName"].ToString()) + " </td>");
Response.Write("<td> " + (rdr["Address"].ToString()) + " </td>");
Response.Write("<td> " + (rdr["City"].ToString()) + " </td>");
Response.Write("<td> " + (rdr["Region"].ToString()) + " </td>");
Response.Write("<td> " + (rdr["PostalCode"].ToString()) + " </td>");
Response.Write("<td> " + (rdr["Country"].ToString()) + " </td>");
Response.Write("<td> " + (rdr["Phone"].ToString()) + " </td>");
Response.Write("<td> " + (rdr["Fax"].ToString()) + " </td></tr>");
//sb.Append(rdr["Company"].ToString());
}
Response.Write("</table>");
}
}




Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
means instead of stringbuilder i want to use table

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response