Link to home
Start Free TrialLog in
Avatar of Gökmen Anıl Yılmaz
Gökmen Anıl Yılmaz

asked on

c# combobox and datetime picker for sql server

hi;
my win form have 2 combobox and 2 datetime picker i and ComboBox2 is filling up according to ComboBox1 no problem but want Filling 2 DateTimePicker according to ComboBox 2

 private void Stok_Load(object sender, EventArgs e)
        {
 SqlDataAdapter sda = new SqlDataAdapter("SELECT DISTINCT MARKA FROM tbl_markamodel", baglanti);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            markacombo.Items.Add("---SELECT---");
            foreach (DataRow ROW in dt.Rows)
            {
                markacombo.Items.Add(ROW["MARKA"].ToString());

            }

        }
  private void markacombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataAdapter sda = new SqlDataAdapter("select distinct model from tbl_markamodel where marka='" +markacombo.Text+"'",baglanti);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            foreach (DataRow ab in dt.Rows)
            {
                modelcombo.Items.Add(ab["model"].ToString());
               
            }
}
Avatar of Geert G
Geert G
Flag of Belgium image

are you sure you want "distinct" in your query ?

some people add distinct to every query ... because someone once said it doesn't harm
but it's usually wrong and a performance problem
Avatar of Norie
Norie

How would the datetime pickers be populated based on what's selected in the second combobox modelcombo?
Avatar of Gökmen Anıl Yılmaz

ASKER

Could you suggest another solution because I do not know  by: Geert G
my database table 4 column marka model date1 date2.

1. ComboBox is filling up the selected 2. ComboBox and  I want to fill the selected 2.combobox with datetimepicker and datetimepicker 2
by: Norie
Will the second combobox be populated with multiple values?

If so, what exactly do you want to show in the datetime pickers?
1. combobox 10 values and 2. combobox i think 50 values.

model production and end date
If there are 50 values in the 2nd combobox it implies they came from 50 records in the database, and each of those records will have 2 time values.

So what value(s) should go in the datetimepickers?

Should they be populated based on which value is selected in the second combobox?

If that's the case I would suggest you populate the second combobox with the values from model and both date columns.
If there are 50 values in the 2nd combobox it implies they came from 50 records in the database, and each of those records will have 2 time values. YES

this pic my table

1.combobox marka

2. combobox model

and 2datetimepicker

i choose combobox 1 value and i fill combobox 2 but i dont  fill datetimepicker
database.png
So if FIAT was selected from the first combobox (markacombo) and 127 from the second combobox (modelcombo) you would want the datetime pickers populated with the dates 1971-06-01 and 1986-02-01?
Yes that's right
but how can i :)
You could use the SelectedIndexChanged event of the 2nd combobox to run another query and return the dates.

Something like this perhaps.
  private void modelcombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataAdapter sda = new SqlDataAdapter("select uretimtarihi, bitisitetarihi from tbl_markamodel where marka='" +markacombo.Text+"' AND model='"+modelcombo.Text +"'",baglanti);
            
            ' code to set values of datetime pickers.
     }

Open in new window

i know but i do not know

this code

I wrote here my fault

            ' code to set values of datetime pickers.

can you say
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank for help and answers