Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MySQL自动完成文本框_C#_.net_Sql_Winforms_Autocomplete - Fatal编程技术网

C# 使用MySQL自动完成文本框

C# 使用MySQL自动完成文本框,c#,.net,sql,winforms,autocomplete,C#,.net,Sql,Winforms,Autocomplete,我试图使用下面的代码来实现文本框的自动完成,但它给出了错误 ERROR :"Object reference not set to an instance of an object" 在这一行: for (int count = 0; count < dt.Rows.Count; count++) for(int count=0;count

我试图使用下面的代码来实现文本框的自动完成,但它给出了错误

ERROR :"Object reference not set to an instance of an object"
在这一行:

for (int count = 0; count < dt.Rows.Count; count++)
for(int count=0;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;
}
private void tbMemberName\u TextChanged\u 1(对象发送方,事件参数e)
{
tbMemberName.AutoCompleteMode=AutoCompleteMode.Suggest;
tbMemberName.AutoCompleteSource=AutoCompleteSource.CustomSource;
AutoCompleteStringCollection name=new AutoCompleteStringCollection();
//字符串搜索=“%”+tbMemberName.Text+“%”;
//字符串@Name=tbMemberName.Text;
字符串sql=
@“从名称为“%”+tbMemberName.Text+'%”的成员中选择不同的(成员_Firstname+“”+member_Lastname)作为名称;
DataTable dt=MemberFormHelper.GetData(sql,mf);
如果(dt.Rows.Count>=0)
{
对于(int count=0;count
如果NRE来自此行

for(int count=0;count
那么这只能意味着
dt
为空

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

尝试

for (int count = 0; count < dt.Rows.Count-1; count++)
for(int count=0;count
如果行计数为10,则需要从0循环到9(10-1)


干杯

有人能帮我摆脱这种情况吗请相应地标记你的问题,尽可能多地使用标记,这将有助于回答你的问题。字符串sql=@“选择(成员名+“”+成员名)作为成员名,其中名称为“”+tbMemberName.Text+”;它不工作,请您确认aboce查询是mysql的正确查询,请。。。它没有给出任何结果error@user1你能发布整个
MemberFormHelper.GetData()
方法吗?公共静态数据表GetData(string sql,formMain mf){string r=WebCalls.webClass.doPost(mf.gBaseUrl+“xxxx/xxxx/query.php”,sql);数据集ds=new DataSet();System.IO.TextReader t=new System.IO.StringReader(r) ;尝试{ds.ReadXml(t);}捕获{Console.WriteLine(r);}如果(ds.Tables.Count>0){DataTable dt=ds.Tables[0];返回dt;}否则{}返回null;}对不起,这似乎是笨拙的,但我没有足够的空间张贴清楚,但这是整个功能getdata@user1如果
ds.Tables.Count
不是
>0
则有一个路径,然后它返回null,我很确定这就是导致
对象引用未设置为对象实例的原因
。如果您认为它不应该返回null,那么我建议您通过该方法进行调试,看看为什么
dt.Tables.Count
不是“>0”。