使用 MySQL 自动完成文本框

发布于 2024-11-15 16:53:49 字数 1071 浏览 1 评论 0原文

我试图使用下面的代码来自动完成文本框,但它

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

在这一行给出了错误:

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

任何人都可以帮助我吗?

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮烟 2024-11-22 16:53:49

如果 NRE 来自此行

for (int count = 0; count < dt.Rows.Count; count++) 则只能意味着 dt 为 null。

因此 MemberFormHelper.GetData(sql, mf); 返回空值。修复 GetData() 或在循环之前检查 dt 是否为 null。

if the NRE is from this line

for (int count = 0; count < dt.Rows.Count; count++) then that can only mean dt is null.

so MemberFormHelper.GetData(sql, mf); is returning a null value. Either fix GetData() or check dt for null before the loop.

冷夜 2024-11-22 16:53:49

尝试

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

如果行数为 10,则需要从 0 循环到 9 (10-1)。

干杯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文