Saturday, 4 April 2015

Ajax for calling and retriving data

No comments
Requesting Page ASPX

<head runat="server">
    <title></title>
    <script type="text/javascript">
           function find_data(fn)
                 {
                     if (window.XMLHttpRequest) {                         //for IE
                         abc = new XMLHttpRequest();
                     }
                     else {
                         try {
                             abc = new ActiveXObject("Msxml2.XMLHTTP");                  //For Other Browser
                         }
                         catch (e) {
                             abc = new ActiveXObject("Microsoft.XMLHTTP");
                         }
                     }
                         url = 'data_find.aspx?fname=' + fn;                                          //Requesting URL
                                                  
                        abc.open('GET',url,true);
                        abc.send();
                       
                        abc.onreadystatechange = function()
                        {
                            if(abc.readyState == 4)
                                {
                                    document.getElementById('view_data').innerHTML=abc.responseText;                                                                   
                                }
                        }
                }                       
    </script>
</head>
<body>
    <form id="form1" runat="server" method="post">
    <div>
        <table border="1" align="center">
            <tr>
                <td>
                    <input type="text" id="fname" onkeyup="find_data(getElementById('fname').value)" />
                </td>
            </tr>
        </table>
    </div>
    </form>
    <div id="view_data">
    </div>
</body>


Requested Page data_find.aspx



 protected void Page_Load(object sender, EventArgs e)
    {
        st1 = Request.QueryString["fname"];
        SqlConnection con = new SqlConnection("connection");
        con.Open();

        //string[] x = st1.Split('_');
        //string y = x[1];

        string str = "select * from AuthorDetail where AuId ='" + st1 + "'";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

       if(dt.Rows.Count>0)
       {
           //string x="img/"+dt.Rows[0]["xol1"].ToString();
           //string y=dt.Rows[0]["bio"].ToString();
           //string xml = "<book img=\"bg.png\"bio=\"hii\"></book>";
          // Response.ContentType = "text/xml";
          // Response.Write(xml);

           Response.Write(dt.Rows[0]["AuthorImage"].ToString());
           Response.Write("|$|");
           Response.Write(dt.Rows[0]["Details"].ToString());
            Response.End();
        }
        //cmd.ExecuteNonQuery();
        con.Close();
       
    }