views:

14

answers:

1

Hi, I'm using an AutoCompleteExtender for an ASP.NET project of mine, and it's pulling the fields to be shown fine. However, when the user chooses an autocomplete suggestion, I need the function to pull another field from the row (the 'ID' column which is the primary key), as that what is the necessary field used to display the relevant data.

Here is my code

public string[] GetAutoComplete(string prefixText)
        {
            int count = 10;
            string sql = "SELECT * FROM SageAccount WHERE Name LIKE @prefixText";
            SqlDataAdapter da = new SqlDataAdapter(sql, "My Connection String(changed due to personal info being shown)");
            da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%";
            DataTable dt = new DataTable();
            da.Fill(dt);
            string[] items = new string[dt.Rows.Count];
            int i = 0;
            foreach (DataRow dr in dt.Rows)
            {
                items.SetValue(dr["Name"].ToString(), i);
                i++;
            }
            return items;
        }

EDIT: Some modified code I am trying:

public string[] GetAutoComplete(string prefixText, int count)
{
    string sql = "SELECT * FROM SageAccount WHERE Name LIKE @prefixText";
    SqlDataAdapter da = new SqlDataAdapter(sql, "Con string");
    da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%";
    DataTable dt = new DataTable();
    da.Fill(dt);
    List<string> Names = new List<string>();
    //string[] items = new string[dt.Rows.Count];
    int i = 0;
    foreach (DataRow dr in dt.Rows)
    {
            Names.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(dr["Name"], dr["ID"].ToString());
        //items.SetValue(dr["Name"].ToString(), i);
    }
    return Names.ToArray();

The issue I have here is that it gives me the error:

The best overloaded method match for 'AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItems(string, string)' has some invalid arguements'

Any ideas? Many thanks

A: 

I've done that in the past using a hidden field to save the selected id. Check out HERE or go over google and check how to use a hidden field, I don't have the code right now.

EDIT 2: Check out that you are expecting an int parameter on your new method, and you are calling sending an string. and .. check that you are calling another method called CreateAutoCompleteItems which it seems to be missings

Darkxes
Cheers buddy. I've updated with some more code because I'm having a little trouble.
Chris