locked
New to Web Service RRS feed

  • Question

  • User-1193791088 posted

    Trying to create a web service for auto complete extender. Saw some tutorials but can't get it to work

    					<ajx:AutoCompleteExtender ID="AC1" runat="server" ServiceMethod="FindItemId" MinimumPrefixLength="2" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" TargetControlID="TextBox1" FirstRowSelected="false" ServicePath="~/FindItemId.asmx"></ajx:AutoCompleteExtender>
    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    
    /// <summary>
    /// Summary description for FindItemId
    /// </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 FindItemId : System.Web.Services.WebService 
    {
    	public FindItemId()
    	{
    
    	}
    
    	[WebMethod]
        public static List<string> GetItemId(string prefixText, int count)
    	{
    		using (SqlConnection conn = new SqlConnection())
    		{
    			conn.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    			using (SqlCommand cmd = new SqlCommand())
    			{
    				cmd.CommandText = "SELECT ItemId FROM CoItems where ItemId like @SearchText + '%'";
    				cmd.Parameters.AddWithValue("@SearchText", prefixText);
    				cmd.Connection = conn;
    				conn.Open();
    				List<string> Items = new List<string>();
    				using (SqlDataReader sdr = cmd.ExecuteReader())
    				{
    					while (sdr.Read())
    					{
    						Items.Add(sdr["ItemId"].ToString());
    					}
    				}
    				conn.Close();
    				return Items;
    			}
    		}
    	}
    }

    <Services>
    				<asp:ServiceReference Path="~/FindItemId.asmx" />
    			</Services>

    How is it supposed to work?

    Monday, September 25, 2017 5:36 AM

All replies

  • User1168443798 posted

    Hi RobertH3,

    >> Saw some tutorials but can't get it to work

    What do you mean by it did not work? Did you get any error? If you put a breakpoint on “return Items” line, did it return the expected values?

    I suggest you try to follow below link.

    # https://social.technet.microsoft.com/wiki/contents/articles/16479.using-ajax-autocompleteextender-with-web-services.aspx

    Best Regards,

    Edward

    Monday, September 25, 2017 6:43 AM
  • User-1193791088 posted

    The Autocomplete Extender did not show, I watched the video but since in videos they never show you how to cross disciplines, i need it to read from a database, not a set of values they give in a video. So nothing happened, which I take that it did not work.

    Monday, September 25, 2017 1:01 PM
  • User1168443798 posted

    Hi RobertH3,

    >> I watched the video but since in videos they never show you how to cross disciplines

    What do you mean by this? Did you check my link? It shared getting data from database.

    >> i need it to read from a database, not a set of values they give in a video

    I am wondering if you return a set of values instead of reading from database, will Autocomplete Extender show up?

    I suggest you put a break point on “GetItemId” to check whether this service is called, and check Items whether it return the right values.

    Best Regards,

    Edward

    Tuesday, September 26, 2017 6:00 AM