rahul patil

rahul patil

  • 1.8k
  • 39
  • 322

Search Record functionality using ajax is not working?

Feb 10 2020 10:42 PM
I m working with ajax.
I want to search the record but my ajax call logic is wrong??
I fetch the record from the database using ajax successfully but search functionality using ajax but I think my ajax call is wrong?
Database field:
table name: tblstud
studid int pk auto increment,
studname varchar(50),
studaddress varchar(50),
procedure for getdatalist:
ALTER PROCEDURE [dbo].[selectstud]
AS
BEGIN
select * from tblstud;
END
Emploee.aspx
 
  1. <body>  
  2.         <form id="form1" runat="server">  
  3.         <div>  
  4.             <table>  
  5.                 <tr>  
  6.                     <td>StudentName:</td>  
  7.                     <td>  
  8.                         <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>  
  9.                 </tr>  
  10.   
  11.                 <tr>  
  12.                     <td>StudentAddress:</td>  
  13.                     <td>  
  14.                         <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox></td>  
  15.                 </tr>  
  16.   
  17.                 <tr>  
  18.                     <td>  
  19.                         <input type="button" id="btnsave" value="Submit" onclick="saverecord()" /></td>  
  20.                 </tr>  
  21.                 <tr>  
  22.                     <td>  
  23.                         Search Record:   <asp:TextBox ID="txtsearch" runat="server"></asp:TextBox>  
  24.                                          <input id="btnsearch" type="button" value="Search" onclick="searchrecord()" />  
  25.                     </td>  
  26.                 </tr>  
  27.                   
  28.             </table>  
  29.         </div>  
  30.   
  31.         <div class="row">  
  32.             <div>  
  33.                 <table id="tbl" border="1">  
  34.                     <thead>  
  35.                         <tr>  
  36.                             <th>StudentName</th>  
  37.                             <th>StudentAddress</th"><br /><br />  
  38.                             <th>Edit</th>  
  39.                         </tr>  
  40.                     </thead>  
  41.                 </table>  
  42.             </div>  
  43.         </div>  
  44.   
  45.         <script type="text/javascript">  
  46.   
  47.             $(document).ready(function () {  
  48.                 GetData();  
  49.             });  
  50.   
  51.             function searchrecord(studname) {                       //here is an problem with search record??  
  52.   
  53.                 if ($("#btnsearch").val() == "Search") {  
  54.                     debugger  
  55.                     $.ajax({  
  56.                         type: "POST",  
  57.                         url: "Emploee.aspx/search",  
  58.                         data: "{ studname:'" + $("#txtsearch").val() + "' }",  
  59.                         contentType: "application/json",  
  60.                         success: function (data) {  
  61.                             debugger  
  62.                             $("#txtsearch").val(data.d);  
  63.                         },  
  64.                     });  
  65.                 }  
  66.             }  
  67.   
  68.             //for getdatalist  
  69.             function GetData() {  
  70.                 $.ajax({  
  71.   
  72.                     url: 'Emploee.aspx/GetData',  
  73.                     type: 'post',  
  74.                     contentType: 'application/json;charset=utf-8',  
  75.                     datatype: 'json',  
  76.                     success: function (data) {  
  77.                         data = JSON.parse(data.d);  
  78.                         $.each(data, function (index, element) {  
  79.                             $("#tbl").append('<td>' + element.studname + '</td>');  
  80.                             $("#tbl").append('<td>' + element.studaddress + '</td>');  
  81.                         });  
  82.                     },  
  83.                     error: function (error) {  
  84.                         alert('Not Get Data')  
  85.                     },  
  86.                 });  
  87.             }  
  88.   
  89.  </script>  
  90.   
  91.  </form>  
  92.   
  93. </body>  
Emploee.aspx.cs
 
  1.        [WebMethod] //for retrieve the data  
  2.           
  3.         public static string GetData()  
  4.         {  
  5.             cn.Open();  
  6.             string data = "";  
  7.             SqlCommand cmd = new SqlCommand("selectstud", cn);  
  8.             cmd.CommandType = CommandType.StoredProcedure;  
  9.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  10.             DataSet ds = new DataSet();  
  11.             da.Fill(ds);  
  12.   
  13.             if (ds.Tables[0].Rows.Count > 0)  
  14.             {  
  15.                 data = JsonConvert.SerializeObject(ds.Tables[0]);  
  16.             }  
  17.             cn.Close();  
  18.   
  19.             return data;  
  20.   
  21.         }  
  22.   
  23.         [WebMethod]  
  24.         public static void search(string studname)                
  25.         {  
  26.             cn.Open();  
  27.   
  28.             SqlCommand cmd = new SqlCommand("SELECT studname FROM tblstud WHERE studname LIKE '%'+@studname+'%'", cn);  
  29.   
  30.             cmd.Parameters.AddWithValue("@studname", studname);  
  31.   
  32.             SqlDataReader reader = cmd.ExecuteReader();  
  33.   
  34.             if (reader.HasRows)  
  35.             {  
  36.                 while (reader.Read())  
  37.                 {  
  38.                     Console.WriteLine("{1}",reader.GetString(1));  
  39.                 }  
  40.             }  
  41.             else  
  42.             {  
  43.                 Console.WriteLine("No rows found.");  
  44.             }  
  45.   
  46.             reader.Close();  
  47.         }  
 
 
 
 
 
see my browser log which place I m going to wrong?
which place I m not correct?help?
 
 

Answers (19)