I've been searching for an answer, but haven't found one (proberly because I'm not familar enough with the right terminology and what to search for). Maybe I've already stumpled over the answer in my search, but just hasn't been able to understand it. I'm trying to populate a dropdownlist in one WinForm, using a Function from another form (edit: class). I've created a class with different functions, connecting, retrieving and manipulating a MySQL database. For this specific question, I need a DropDownList to populate with data retrieved from specific column in a specific database table, using the function, but I can't really wrap my head around what the data should stored in, and how to pass it to the DropDownList. Tried different things, like IList, and making the Funtion an Array. My logic tells my that the data should be stored like an Array, and the DropDownList should be populated with a ForEach Loop. The SQL part I'm okay with. It's just the logic with the storing I'm uncertain of. My Function looks like this at this time:
public IList<string> Populate_DropDowns(string Table, string Column_Name)
{
string query = "SELECT * FROM " + Table;
IList<string> Populate_DropDowns = new List<string>();
if (this.Open())
{
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader dataReader = cmd.ExecuteReader();
try
{
while (dataReader.Read())
{
Populate_DropDowns.Add(dataReader[Column_Name].ToString()); dataReader.Close();
}
}
catch { }
this.Close();
return Populate_DropDowns;
}
else
{
return Populate_DropDowns;
}
}
In the Form with the DropDownList I have following code:
protected virtual void OnLoad(EventArgs e)
{
DropDownList_Select_Template.Items.Add(sqlClient.Populate_DropDowns(Table, Column_Name));
}
I'm using C# WinForms.
Please advise. Any help is appreciated.
Best regards
Frank