Israel

Israel

  • 702
  • 1.3k
  • 216.4k

How to make filter into combobox from Database properly

Apr 19 2017 3:40 AM
Hi!
 
I have database with two column: "name" and "mobile". I need to filter for example on the column "mobile" a specific number. Then into my combobox should appear all names affected by the same mobile number only. But the code I wrote doesnt obey. Its point the error here: SqlDataReader sdr = comm.ExecuteReader();
 
 
private void cbxName_SelectedIndexChanged(object sender, EventArgs e)
conn.Open();
//comm = new SqlCommand("SELECT DISTINCT nom_complet FROM church", conn);
comm = new SqlCommand("SELECT name FROM church where mobile like('" + "txtMobileNumber.Text" + "%')");
SqlDataReader sdr = comm.ExecuteReader();//Its point here as error
DataTable dt = new DataTable();
dt.Load(sdr);
cbxReadNomFlashMessage.DisplayMember = "name";
cbxReadNomFlashMessage.DroppedDown = true;
List<string> list = new List<string>();
foreach (DataRow row in dt.Rows)
{
list.Add(row.Field<string>("name"));
}
this.cbxReadNomFlashMessage.Items.AddRange(list.ToArray<string>());
cbxReadNomFlashMessage.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cbxReadNomFlashMessage.AutoCompleteSource = AutoCompleteSource.ListItems;
conn.Close();
///////////////////////doesnt allow repeat item in combobox////////////
for (int i = 0; i < cbxReadNomFlashMessage.Items.Count; i++)
{
for (int y = 0; y < cbxReadNomFlashMessage.Items.Count; y++)
{
if (y != i && cbxReadNomFlashMessage.Items[i].ToString() == cbxReadNomFlashMessage.Items[y].ToString())
{
cbxReadNomFlashMessage.Items.RemoveAt(i);
break;
}
}
}
 

Answers (5)