AutoComplete IndexOutofRangeException

时间:2014-08-26 09:41:31

标签: c# autocomplete

我是C#的初学者.Help将不胜感激。 我在FoodList.Add(dt.Rows [i] [1] .ToString())中遇到错误; 我试图从数据库中进行自动完成,但我可以在数据表中获取数据,但它没有显示并给我(IndexOutOfRangeException)。

protected void Page_Load(object sender, EventArgs e)
    {  }  
    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
public static List<string> GetFood(string prefixText)
{
    DataTable dt = new DataTable();
    string constr = ConfigurationManager.ConnectionStrings["FoodCompass"].ToString();
    SqlConnection con = new SqlConnection(constr);
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT * FROM [FoodList] WHERE ([Food] LIKE @Food + '%')", con);
    cmd.Parameters.AddWithValue("@Food", prefixText);
    SqlDataAdapter adp = new SqlDataAdapter(cmd);       
    adp.Fill(dt);
    List<string> FoodList = new List<string>();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
       FoodList.Add(dt.Rows[i][1].ToString());
    }
    return FoodList;
}

这是我希望它实现的文本框的来源。

<asp:TextBox ID="tbxBreakfast" runat="server" placeholder="FoodConsumed" BackColor="#E8E8E3" BorderColor="#CCCCCC" BorderStyle="Inset" Font-Names="Eraser" Height="20px" Width="160px"></asp:TextBox>
            <ajaxToolkit:AutoCompleteExtender ID="tbxBreakfast_AutoCompleteExtender" runat="server" DelimiterCharacters="" Enabled="True" MinimumPrefixLength="1" EnableCaching="true" CompletionInterval="1000" ServiceMethod="GetFood" TargetControlID="tbxBreakfast" UseContextKey="True">
            </ajaxToolkit:AutoCompleteExtender>

0 个答案:

没有答案