Textbox autocomplete with mysql as database
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 开发者_如何学Go= 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;
}
dt
is null, prolly (unless u have a record in ur table with the following name 'tbMemberName.Text')... I guess so - tbMemberName
is a TextBox
, so if you are trying to pass its value to the sql string instead of
@"SELECT DISTINCT(member_Firstname +''+ member_Lastname) AS Name FROM members WHERE Name Like '%'+tbMemberName.Text+'%'";
u have to write
@"SELECT DISTINCT(member_Firstname +''+ member_Lastname) AS Name FROM members WHERE Name Like '%"+tbMemberName.Text+"%'";
u have just misused quotation marks.
I used this code based on the codes of msdn microsoft and of sir P.K. i think you should consider this
First I insert these codes in the form_load
ex:
this.cmbSchool.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.cmbSchool.AutoCompleteSource = AutoCompleteSource.CustomSource;
To access the textchanged property in my example I used comboBox instead of Textbox (I inserted them outside forms and used it as universal var to be accessed by combobox1_textchange later)
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
if you are using Mysql I used this (also inserted in form_load) to save the data in collection
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();
Then the last step is I inserted this code in cmbSchool_TextChanged
this.cmbSchool.AutoCompleteCustomSource = collection; //everytime you type it will initiate and gather data from the collection
P.S. this is my first time to post sorry for my explanation and bad coding but hope it helps
I think DT must be null and its actually the line above the for loop which fails
精彩评论