omar sabatin

omar sabatin

  • 1.9k
  • 120
  • 1.2k

how to retrieve data using ajax in c# ASP.NET ?

Aug 15 2020 2:09 AM
im trying to retrieve data using ajax and  bound it to a dropdownlist
i have created a web method in remin.aspx form as follows :
  1. [WebMethod]  
  2.        public static List<senders_list> GetCustomers()  
  3.        {  
  4.            string query = "select c_no, c_name from cust";  
  5.            string constr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;  
  6.              
  7.            using (OracleConnection con = new OracleConnection(constr))  
  8.            {  
  9.                using (OracleCommand cmd = new OracleCommand(query, con))  
  10.                {  
  11.                    List<senders_list> customers = new List<senders_list>();  
  12.                    cmd.CommandType = CommandType.Text;  
  13.                    cmd.Connection = con;  
  14.                    con.Open();  
  15.   
  16.                    OracleDataAdapter da = new OracleDataAdapter(cmd);  
  17.                    DataTable dt = new DataTable();  
  18.                    da.Fill(dt);  
  19.                    if (dt.Rows.Count>0) {  
  20.                        for (int i=0;i<dt.Rows.Count;i++) {  
  21.                            customers.Add(new senders_list  
  22.                            {  
  23.                                c_no = dt.Rows[i]["c_no"].ToString(),  
  24.                                c_name = dt.Rows[i]["c_name"].ToString()  
  25.                            });   
  26.                        }   
  27.                    }  
  28.                    return customers;  // i have created breakpoint here and it return the data fine
  29.                }  
  30.            }  
  31.        } 
 and this is the class :
  1. public class senders_list  
  2.        {  
  3.            public string c_no { get; set; }  
  4.            public string c_name { get; set; }  
  5.        } 
and im calling the ajax request from test.aspx
this is the test.aspx ,ajax call
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WEBDBIRAQI.test1" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
  8.       <script type="text/javascript">  
  9.           (document).ready(function () {  
  10.               $.ajax({  
  11.                   type: "POST",  
  12.                   contentType: "application/json; charset=utf-8",  
  13.                   url: "remin.aspx/GetCustomers",  
  14.                   data: "{}",  
  15.                   dataType: "json",  
  16.                   success: function (Result) {  
  17.                       Result = Result.d;  
  18.                       $.each(Result, function (key, value) {  
  19.                           $("#ss_list").append($("<option></option>").val  
  20.                           (value.C_NO).html(value.C_NAME));  
  21.                       });  
  22.                         
  23.                   },  
  24.                   error: function (Result) {  
  25.                       alert("Error");  
  26.                   }  
  27.               });  
  28.           });  
  29.     </script>  
  30.     <title></title>  
  31. </head>  
  32. <body>  
  33.     <form id="form1" runat="server">  
  34.     <div>  
  35.     <asp:dropdownlist id="ss_list"   runat="server" ></asp:dropdownlist>  
  36.     </div>  
  37.     </form>  
  38. </body>  
  39. </html> 
when i run the form it shows only the dropdownlist but with no data inside !
im sure that the problem is with my ajax call Code ,because i have created a break point in the web method  and it returned the data fine ! 
i need help please !

Brought to you by:

Answers (6)