-4

I'm using below code but, it's giving me an error.

private void ma2()
{
    try
    {  
        string query = "select  k7 from kholy1";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.CommandText = query;
        con.Open();
        SqlDataReader drd = cmd.ExecuteReader();
        while (drd.Read())
            {
            comboBox5.Items.Add(drd.GetValue(0).ToString());
        }
        drd.Close();
    }
    catch
    {
        MessageBox.Show("Error ");
    }
}

I'm getting an error while displaying the form!

Armin
  • 220
  • 2
  • 10
  • Could you please properly format the question? Then Include the error message as well to get better solutions from our site – sujith karivelil Mar 09 '17 at 05:32
  • What's the error? – Armin Mar 09 '17 at 05:37
  • display MessageBox.Show("Error "); – Mahmoud Magdy Mousaa Mar 09 '17 at 05:38
  • 3
    Update your `catch` to `catch (Exceptione)` and the do `MessageBox.Show(e.Message);` to get the actual error message. Take a look here http://stackoverflow.com/questions/18823668/show-message-box-in-case-of-exception – Armin Mar 09 '17 at 05:43
  • Error is the message on catch phrase but was is the actual error? Also what part in your code is `con`? We can only see `con.open()` but no `SqlConnection` etc etc. – P. Pat Mar 09 '17 at 05:43

1 Answers1

-1

You just simply use combo box tasks and check use Data Bound Items that's how you are able to connect it to sql database. Value member use to store it's value in database and Display Member use to Display value in front end.

You can use selected value using combobox1.SelectedValue.

Reference- Binding WPF ComboBox to a Custom Listenter image description here

Community
  • 1
  • 1
Utkarsh Dubey
  • 703
  • 11
  • 31