Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
Webservices code is working proper but in textbox not fetching data from database.And no error displaying..



webservices code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Collections.Generic;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    //public string HelloWorld() {
    //    return "Hello World";
    //}



 public List<string> GetCountries(string prefixText)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select * from ace where name like @name+'%'", con);
cmd.Parameters.AddWithValue("@name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable  dt = new DataTable( );
da.Fill(dt);
List<string> CountryNames = new List<string>();
for(int i=0;i<dt.Rows.Count;i++)
{
CountryNames.Add(dt.Rows[i][1].ToString());
}
return CountryNames;
}
}


default.aspx code



        <asp:TextBox ID="txtCountry" runat="server">  <br />
         
       
        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtCountry" ServicePath="WebService.asmx"
MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="GetCountries" OnClientPopulating="ShowProcessImage" OnClientPopulated="HideProcessImage">
Posted

1 solution

change your method signature as below
C#
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetCountries(string prefixText, int count)
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
 con.Open();
 SqlCommand cmd = new SqlCommand("select * from ace where name like @name+'%'", con);
 cmd.Parameters.AddWithValue("@name", prefixText);
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 DataTable  dt = new DataTable( );
 da.Fill(dt);
 List<string> CountryNames = new List<string>();
 for(int i=0;i<dt.Rows.Count;i++)
 {
   CountryNames.Add(dt.Rows[i][1].ToString());
 }
 return CountryNames;
}

check the documentation for more information[^]
 
Share this answer
 
v2
Comments
mazharkhan123 22-Apr-15 1:05am    
After putting your code webservice also not working.. means GetCountriess method not display in webservice..
mazharkhan123 22-Apr-15 1:53am    
I am getting following message in webservices and when i am removing static then again method showing in webservice..

The following operations are supported. For a formal definition, please review the Service Description.

This web service is using http://tempuri.org/ as its default namespace.

Recommendation: Change the default namespace before the XML Web service is made public.

Each XML Web service needs a unique namespace in order for client applications to distinguish it from other services on the Web. http://tempuri.org/ is available for XML Web services that are under development, but published XML Web services should use a more permanent namespace.

Your XML Web service should be identified by a namespace that you control. For example, you can use your company's Internet domain name as part of the namespace. Although many XML Web service namespaces look like URLs, they need not point to actual resources on the Web. (XML Web service namespaces are URIs.)

For XML Web services creating using ASP.NET, the default namespace can be changed using the WebService attribute's Namespace property. The WebService attribute is an attribute applied to the class that contains the XML Web service methods. Below is a code example that sets the namespace to "http://microsoft.com/webservices/":

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900