使用 MySQL 自动完成文本框
我试图使用下面的代码来自动完成文本框,但它
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 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 meandt
is null.so
MemberFormHelper.GetData(sql, mf);
is returning a null value. Either fixGetData()
or checkdt
for null before the loop.尝试
如果行数为 10,则需要从 0 循环到 9 (10-1)。
干杯
Try
If the row count is 10, you need to loop from 0 to 9 (10-1).
Cheers