Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I wants to retrieve data from sql server using webserver/jquery and I read many articles but not getting the require matters except below but I think It's support FrameWork .Net 3.5 not 2.0. And I have the same requirement to use it in ASP.Net 2.0.

//On WebServer Page..

WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]

public class test : System.Web.Services.WebService
{

 [WebMethod]
 public string GetCustomer(string memberID)
 {
  string response = "<p>No customer selected</p>";
  string connect = "Server=myserver;Initial Catalog=mydatabase;uid=myuser;pwd=mypassword";
  string query = "SELECT name, father, mother from samaj where name=@memberID";

  if (memberID != null)
  {
   StringBuilder sb = new StringBuilder();
   using (SqlConnection conn = new SqlConnection(connect))
   {
    using (SqlCommand cmd = new SqlCommand(query, conn))
   {
    cmd.Parameters.AddWithValue("memberID", memberID);
    conn.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    if (rdr.HasRows)
    {
     while (rdr.Read())
    {
     sb.Append("<p>");
     sb.Append("<strong>" + rdr["name"].ToString() + "</strong><br />");
     sb.Append(rdr["father"].ToString() + "<br />");
     sb.Append(rdr["mother"].ToString() + "<br />");
     response = sb.ToString();
     }
     }
     }
     }
     }
     return response;
     }
     }
     } 
//.aspx page...
<script type="text/javascript" src="jquery-1.3.2.min.js" ></script>
<script type="text/javascript" >
$(document).ready(function(){
$("#Customers").change(function() 
{ 
 $.ajax
 ({
  type: "POST", 
  contentType: "application/json; charset=utf-8", 
  url: "test.asmx/GetCustomer", 
  data: "{ memberID: '" + $('#Customers').val() + "'}", 
  dataType: "json", 
  success: function(data) 
  { 
   $("#CustomerDetails").html(data.d); 
   } 
   }); 
   }); 
});
</script>
<form id="form1" runat="server">
<div id="SelectCustomers"> 
<asp:DropDownList ID="Customers" runat="server"> 
</asp:DropDownList> 
</div> 
<div id="CustomerDetails">
</div>
</form>


The DropdownList Binding In Default.aspx.cs page...
protected void Page_Load(object sender, EventArgs e)
{
 string connect = "Server=myserver;Initial Catalog=mydatabse;uid=myuser;pwd=mypwd 
 string query = "SELECT name FROM samaj"; 
 using (SqlConnection conn = new SqlConnection(connect)) 
 { 
  using (SqlCommand cmd = new SqlCommand(query, conn)) 
 { 
   conn.Open(); 
 Customers.DataSource = cmd.ExecuteReader(); 
 Customers.DataValueField = "name"; 
 Customers.DataBind(); 
 } 
 }

.asmx page testing ok It’s retrieves the data well but on client side It’s not return any data. How to achieve it in ASP.Net 2.0?.

share|improve this question
    
Looks about right... maybe a slight syntax error somewhere. If you debug the app, when you change the item in the #Customers dropdown does any network activity happen? Does it even call the Web Method at all? –  Ben Nov 19 '13 at 10:53
    
@Ben, no network activity nor Web Method Call?. I didn't saw any activity happen on network. –  user2955849 Nov 19 '13 at 13:43
    
In that case there must be a problem with the script - A few things to check: jQuery is loaded, #Customers id is set as expected - if not you need to use $('#<%= Customers.ClientID %>').change(... –  Ben Nov 19 '13 at 14:16
    
@Ben, I think (data.d) is specially for ASP.Net 3.5 possiblility not to support on ASP.Net 2.0 and I am using ASP.Net 2.0 is n't it?. –  user2955849 Nov 20 '13 at 5:57
    
I have already applied $("#<%=Customers.ClientID%>") but no use.... –  user2955849 Nov 20 '13 at 6:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.