文本框自动完成与MySQL [英] Textbox autocomplete with MySQL

查看:159
本文介绍了文本框自动完成与MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的code做文本框自动完成,但它给错误

 错误:对象未设置为一个对象的实例
 

在这条线:

 的(诠释计数= 0; COUNT< dt.Rows.Count;计数++)
 

谁能帮帮我吗?

 私人无效tbMemberName_TextChanged_1(对象发件人,EventArgs的)
{
    tbMemberName.AutoCompleteMode = AutoCompleteMode.Suggest;
    tbMemberName.AutoCompleteSource = AutoCompleteSource.CustomSource;
    AutoCompleteStringCollection namec =新AutoCompleteStringCollection();

    //字符串搜索=%+ tbMemberName.Text +%;
    //字符串@Name = tbMemberName.Text;
    字符串SQL =
        @SELECT DISTINCT(member_Firstname +''+ member_Lastname)从成员的WHERE名称,比如名称'%'+ tbMemberName.Text +'%';
    数据表DT = MemberFormHelper.GetData(SQL,MF);
    如果(dt.Rows.Count> = 0)
    {
        对于(诠释计数= 0; COUNT< dt.Rows.Count;计数++)
        {
            namec.Add(dt.Rows [统计] [名]的ToString());
        }
    }
    tbMemberName.AutoCompleteCustomSource = namec;
}
 

解决方案

尝试

 的(诠释计数= 0; COUNT< dt.Rows.Count-1;计数++)
 

如果该行数是10,你从0需要循环到9(10-1)。

干杯

I am trying to do the text box autocomplete by using the code below, but it gives the error

ERROR :"Object reference not set to an instance of an object"

on this line:

for (int count = 0; count < dt.Rows.Count; count++)

Can anyone please help me?

private void tbMemberName_TextChanged_1(object sender, EventArgs e)
{
    tbMemberName.AutoCompleteMode = AutoCompleteMode.Suggest;
    tbMemberName.AutoCompleteSource = AutoCompleteSource.CustomSource;
    AutoCompleteStringCollection namec = new AutoCompleteStringCollection();

    //string search ="%"+ tbMemberName.Text +"%";
    //string @Name = tbMemberName.Text; 
    String sql =
        @"SELECT DISTINCT(member_Firstname +''+ member_Lastname) AS Name FROM members WHERE Name  Like '%'+tbMemberName.Text+'%'";
    DataTable dt = MemberFormHelper.GetData(sql, mf);
    if (dt.Rows.Count >= 0)
    {
        for (int count = 0; count < dt.Rows.Count; count++)
        {
            namec.Add(dt.Rows[count][Name].ToString());
        }
    }
    tbMemberName.AutoCompleteCustomSource = namec;
}

解决方案

Try

for (int count = 0; count < dt.Rows.Count-1; count++)

If the row count is 10, you need to loop from 0 to 9 (10-1).

Cheers

这篇关于文本框自动完成与MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆