Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello guys,
I have this code which should read data type Time and then SelectItem,but it doesn't select Item. How should I change it ? thanks!
This is how I fill it:
C#
void vypl_comboboxy()
        {
            

                spojeni.Open();
                var cb1 = new SqlCommand("SELECT odjezd FROM stanice WHERE akce='"
                                + selectrowid+ "' ORDER BY odjezd ASC", spojeni);
 SqlDataReader dr1 = cb1.ExecuteReader();
                while (dr1.Read())
                {
                    comboBox1.Items.Add(dr1["odjezd"]);

                }
                dr1.Close();
                dr1.Dispose();

This is how I want to select item in data type Time
C#
SqlCommand novyprikaz = new SqlCommand("SELECT * FROM klient WHERE ID_K=" + selectrowid, spojeni);
                spojeni.Open();
                SqlDataReader precti = novyprikaz.ExecuteReader();

                if (precti.Read())
                {
 comboBox1.SelectedItem = precti.GetTimeSpan(16).ToString();
}
Posted
Updated 15-Jul-13 23:42pm
v2
Comments
Naz_Firdouse 16-Jul-13 6:44am    
are you sure that the value precti.GetTimeSpan(16).ToString() is added to the Combobox???
if it is listed in the combobox , it will be selected.
mareksip 16-Jul-13 6:50am    
It is added. It shows the whole combobox but don't select the right one. It is empty when I load form

1 solution

You are adding Object type in combobox and setting string type as selected Item. It won't work.

Add string type in combo box like

C#
comboBox1.Items.Add(dr1["odjezd"].ToString());


and also the selectedItem as string

comboBox1.SelectedItem = precti.GetTimeSpan(16).ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900