Reputation:
I am trying to autocomplete textbox, and i downloaded source code from any website, Here is aspx page code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ajax AutoCompleteExtender without Webservice</title>
</head>
<body>
<form id="form1" runat="server">
<ajax:ToolkitScriptManager ID="ScriptManager1" runat="server"/>
<div>
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<ajax:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="txtCountry"
MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1"
CompletionInterval="1000" ServiceMethod="GetCountries" >
</ajax:AutoCompleteExtender>
</div>
</form>
and here is codebehind file.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetCountries(string prefixText)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select varStateName from tblStateMaster where varStateName 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;
}
}
It could not generate proper output and i am not able to debug this service, so how can i debug such service, and please tell me where is exact problem and which points should be remember for service and autocomplete, And one more thing can i do it with dropdownlist?
Upvotes: 0
Views: 183
Reputation: 15138
I suspect the issue is in your query and more specifically in the %
, try to include it in your parameter instead:
prefixText = string.Concat(prefixText, "%");
SqlCommand cmd = new SqlCommand("select varStateName from tblStateMaster where varStateName like @Name", con);
EDIT
Ok, so digging around, according to official examples from Microsoft, your method signature should look like this (notice the additional parameters):
public static string[] GetCountries(string prefixText)
{
// your stuff
}
Upvotes: 1