Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I there,

I'm trying to create a AutoCompleteExtender in a textbox but nothing happens when i write something on it.

My asp.net code its this:

ASP.NET
<<pre lang="xml">asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Height="21px" Width="477px" AutoPostBack="true"></asp:TextBox>
                <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" ServicePath="~/AutoComplete.asmx"
                    ServiceMethod="AutoCompleteSearch" TargetControlID="TextBox1" MinimumPrefixLength="1" CompletionSetCount="10" >
                </asp:AutoCompleteExtender>
            </ContentTemplate>
        </asp:UpdatePanel></pre>


and my web service its this one:

C#
[WebMethod]
  public string[] AutoCompleteSearch(string prefixText, int count)
  {

      DataSet ds = new DataSet();
      SqlDataAdapter sda = new SqlDataAdapter();
      sda.SelectCommand = new SqlCommand();
      sda.SelectCommand.Connection = new SqlConnection("PHCConnectionString");
      if (sda.SelectCommand.Connection.State == ConnectionState.Closed)
          sda.SelectCommand.Connection.Open();
      sda.SelectCommand.CommandType = CommandType.Text;
      sda.SelectCommand.CommandText = "Select (nome,' ',bino) as u_nome From cve ";
      sda.Fill(ds, "data");
      sda.SelectCommand.Connection.Close();

      string[] main = new string[0];
      for (int i = 0; i < ds.Tables[0].Rows.Count - 1;i++ )
      {
          if (ds.Tables[0].ToString().ToLower().StartsWith(prefixText.ToLower()))
          {
              Array.Resize(ref main, main.Length + 1);
              main[main.Length - 1] = ds.Tables[0].Rows[i].ItemArray[0].ToString();
              if (main.Length == 20)
                  break;
          }
      }
      Array.Sort(main);
      return main;
  }


can someone help me understand what i did wrong??
Posted

1 solution

"Select (nome,' ',bino) as u_nome From cve "

That SQL query doesn't look right - if you're using MS SQL, you can't just wrap multiple values in brackets and expect SQL to concatenate them.
if (ds.Tables[0].ToString().ToLower().StartsWith(prefixText.ToLower()))

You're also only returning any data if the name of the first table starts with the prefix - you should be comparing the value instead.

Rather than reading every record from the table into memory and then using C# to filter and sort them, you should use SQL:
C#
[WebMethod]
public string[] AutoCompleteSearch(string prefixText, int count)
{
    using (SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE"))
    using (SqlCommand command = new SqlCommand("SELECT TOP (@Count) nome + ' ' + bino As u_nome From cve Where nome + ' ' + bino Like @Prefix Order By nome, bino", connection))
    {
        command.Parameters.AddWithValue("@Prefix", prefixText + "%");
        command.Parameters.AddWithValue("@Count", count);
        
        connection.Open();
        using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleResult))
        {
            List<string> result = new List<string>(count);
            while (reader.Read())
            {
                result.Add(reader.GetString(0));
            }
            
            return result.ToArray();
        }
    }
}
 
Share this answer
 
v2

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