0

I don't really know how to make a specific question of this in the title but. How can I get the Lname, Fname and Mname to string from the comboBox selectedItem. This is my idea but it won't work :/

private void FillWinemakerComboBox()
    {
        SqlCommand cmand = new SqlCommand("SELECT (Lname+', '+Fname+' '+Mname) AS combinedName, Lname, Fname, Mname FROM Winemaker", con);
        con.Open();
        SqlDataReader sqlReader = cmand.ExecuteReader();

        while (sqlReader.Read())
        {
            //This is where the comboBox fills the names
            winemaker_comboBox.Items.Add(sqlReader["combinedName"].ToString());

            //The part where I don't really know how to get the other values
            if (winemaker_comboBox.SelectedItem != DBnull.Value)
            {
                string lastname = sqlReader["Lname"].ToString();
                string firstname = sqlReader["Fname"].ToString();
                string middlename = sqlReader["Mname"].ToString();

            }
        }

        sqlReader.Close();
        con.Close(); 
    }
  • you have to do like this ((sqlreader)winemaker_comboBox.SelectedItem )["Lname"] – rashfmnb Mar 31 '16 at 13:38
  • check this http://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source – teran Mar 31 '16 at 13:47
  • This question doesn't make sense you already have the Lname, Fname, and Mname in the sqlReader, why do you need to get them from the combo selected Item? – James Dev Mar 31 '16 at 14:06
  • @rashfmnb Thank you but where can I use it? – dwinpls Mar 31 '16 at 14:19
  • @James Dev Because I need it to get what the user specifically selected? – dwinpls Mar 31 '16 at 14:19
  • inside your if statement where you want to store values – rashfmnb Mar 31 '16 at 14:23
  • @dwinpls I have updated my original answer. You will have to do some string manipulation. But you are filling the combo box and then trying to find the selected item value in the same method. This won't return what the user selected. – James Dev Mar 31 '16 at 14:26

1 Answers1

0

What I would do instead is fill your combo box with a Text and Value object, this way you can store the names in the Value and retrieve them easily:

private void FillWinemakerComboBox()
{
    SqlCommand cmand = new SqlCommand("SELECT (Lname+', '+Fname+' '+Mname) AS combinedName, Lname, Fname, Mname FROM Winemaker", con);
    con.Open();
    SqlDataReader sqlReader = cmand.ExecuteReader();
    Dictionary<string, string> comboSource = new Dictionary<string, string>();

    while (sqlReader.Read())
    {
        var key = String.Format("{0},{1},{2}", sqlReader["Lname"].ToString(), sqlReader["Fname"].ToString(), sqlReader["Mname"].ToString());
        comboSource.Add(key, sqlReader["combinedName"].ToString());
    }

    winemaker_comboBox.DataSource = new BindingSource(comboSource, null);
    winemaker_comboBox.DisplayMember = "Value";
    winemaker_comboBox.ValueMember = "Key";
    sqlReader.Close();
    con.Close();
}

You fill your combo box like this and then retrieve the items like this:

if (winemaker_comboBox.SelectedItem != DBnull.Value)
{
    var names = winemaker_comboBox.SelectedValue.ToString().Split(',');
    string lastname = names[0];
    string firstname = names[1];
    string middlename = names.Length > 2 ? names[2] : string.Empty;
}
James Dev
  • 2,979
  • 1
  • 11
  • 16
  • The prob is I didn't put a comma in the middle of my first name and middle name so the splitting will be just Lastname and Firstname + Middlename – dwinpls Mar 31 '16 at 13:56
  • Oh thank you but the next problem is what if First Name has a space like "John Patrick"? Then what now? – dwinpls Mar 31 '16 at 14:38
  • I wouldn't do any string manipulation but I would suggest you work with the Id's instead. You can populate the combo box value with the Winemaker Id and then use this to query the database for the names. – James Dev Mar 31 '16 at 14:54
  • I've update my answer with an alternative solution. It's a little crude but will do the job. One issue may be that the Dictionary key needs to be unique so will throw an exception if 2 people have exactly the same name but you can fix this by adding the Winemaker Id into the key or something. – James Dev Mar 31 '16 at 15:48
  • Speaking of winemaker ID I kinda changed my idea I'm so sorry you've made an effort but thank you so much. My idea was, after the combobox fills with the text combinedName and then combinedName's values will be the winemakerID. To make the search easier. – dwinpls Mar 31 '16 at 19:06