0

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

4
  • Possible duplicate: stackoverflow.com/questions/7423911/… Commented Feb 27, 2015 at 21:39
  • Also refer to stackoverflow.com/questions/600869/… Commented Feb 27, 2015 at 21:40
  • Thank you very much. I'll dig into those. Sorry for the duplicate. Not sure whether this question should/will be deleted? Br Frank. Commented Feb 28, 2015 at 14:33
  • I managed to solve my problem with arrays. Not sure if it's the best way, but it worked and I understood it :) Commented Mar 23, 2015 at 20:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.