AJAX Database Example in Asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimpleAjax.aspx.cs" Inherits="SimpleAjax" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>

  <script type="text/javascript">
  var xmlHttp

function showCustomer(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="getcustomer.aspx";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
}
return xmlHttp;
}
  </script>

</head>
<body>
  <form id="form1" runat="server">
      Select a Customer:
      <select name="customers" onchange="showCustomer(this.value)">
          <option value="ALFKI">
          Alfreds Futterkiste
          <option value="NORTS ">
          North/South
          <option value="WOLZA">
          Wolski Zajazd
      </select>
      <div id="txtHint">
          <b>Customer info will be listed here.</b></div>
  </form>
</body>
</html>
GetCustomer.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetCustomer.aspx.cs" Inherits="GetCustomer" %>

using System;
using System.Data;
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.Data.SqlClient;
using System.Text;

public partial class GetCustomer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strId = Request.QueryString["q"].ToString();
        SqlConnection con = new SqlConnection("server=(local);database=northwind;uid=sa;pwd=12345");
        SqlDataAdapter da = new SqlDataAdapter("select * from customers where customerid='" + strId + "'", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        StringBuilder sb = new StringBuilder();
        sb.Append("<table border='1'><tr><th>CompanyName</th><th>contactname</th><th>Contacttitle</th><th>Address</th></tr>");


        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            sb.Append("<tr><td>" + dr["CompanyName"].ToString() + "</td>");
            sb.Append("<td>" + dr["contactname"].ToString() + "</td>");

            sb.Append("<td>" + dr["Contacttitle"].ToString() + "</td>");
            sb.Append("<td>" + dr["Address"].ToString() + "</td></tr>");
        }

        sb.Append("</table>");
        Response.Write(sb.ToString());
    }
}

1 comment: