Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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#_Mysql_Winforms_Autocomplete_Textbox - Fatal编程技术网

C# 使用mysql作为数据库的文本框自动完成

C# 使用mysql作为数据库的文本框自动完成,c#,mysql,winforms,autocomplete,textbox,C#,Mysql,Winforms,Autocomplete,Textbox,我试图使用下面的代码来实现文本框的自动完成,但它给出了错误 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我使用了基于MSDN微软代码和P.K.爵士的代码,我认为你应该考虑这个 首先,我以加载的形式插入这些代码 例如: 在我的示例中,为了访问textch

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

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
我认为DT必须为null,并且它实际上是for循环上方失败的那一行

DT
为null,prolly(除非您的表中有一条记录的名称为'tbMemberName.Text')。。。我猜so-
tbMemberName
是一个
TextBox
,因此如果您试图将其值传递给sql字符串而不是

@“从名为“%”+tbMemberName.Text+'%”的成员中选择不同的(成员_Firstname+“”+member_Lastname)作为名称

你必须写作

@“从名为“%”+tbMemberName.Text+“%”的成员中选择不同的(成员_Firstname+“”+member_Lastname)作为名称


u只是误用引号。

< p>我使用了基于MSDN微软代码和P.K.爵士的代码,我认为你应该考虑这个

首先,我以加载的形式插入这些代码
例如:

在我的示例中,为了访问textchanged属性,我使用了comboBox而不是Textbox(我在表单外部插入了它们,并将其用作combobox1\u textchange稍后访问的通用变量)


如果您使用的是Mysql,那么我使用它(也插入到form_load中)将数据保存在集合中

        string querySelect = "SELECT * FROM tblschools";
        MySqlCommand commandSelect = new MySqlCommand(querySelect, connectionMain);
        MySqlDataReader reader = commandSelect.ExecuteReader();
        while (reader.Read())
        {
            string type = reader[1].ToString();
            cmbSchool.Items.Add(type); //data inserted in combobox list (dropdownstyle in c# dropdown) so that I can still type
            collection.Add(type); //data inserted in collection so that it will be autocomplete when you type keywords
        }
        reader.Close();
最后一步是在CMBU文本中插入此代码

this.cmbSchool.AutoCompleteCustomSource = collection; //everytime you type it will initiate and gather data from the collection

顺便说一句,这是我第一次发帖,对我的解释和糟糕的编码表示抱歉,但希望能有所帮助

你确定
dt.Rows
不是空的吗?
dt
可能是空的,这就是为什么错误数据没有加载到此statementnamecollection的名称集合中。添加(dt.Rows[0][“member\u Firstname”].ToString();你能帮我一下吗?你终于进入了
for
循环。如果您仍在使用
选择DISTINCT(member\u Firstname+“”+member\u Lastname)作为名称
语句
dt。行[0][“member\u Firstname”]
将失败。因为没有
“member\u Firstname”
列。您要寻址的列是“Name”(您给出的别名),因此请改用
dt.Rows[0][“Name”]
。如果仍然存在问题,请发布新的
选择
语句
        string querySelect = "SELECT * FROM tblschools";
        MySqlCommand commandSelect = new MySqlCommand(querySelect, connectionMain);
        MySqlDataReader reader = commandSelect.ExecuteReader();
        while (reader.Read())
        {
            string type = reader[1].ToString();
            cmbSchool.Items.Add(type); //data inserted in combobox list (dropdownstyle in c# dropdown) so that I can still type
            collection.Add(type); //data inserted in collection so that it will be autocomplete when you type keywords
        }
        reader.Close();
this.cmbSchool.AutoCompleteCustomSource = collection; //everytime you type it will initiate and gather data from the collection