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());
               
            }
}
Gökmen Anıl YılmazStudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert GOracle dbaCommented:
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
NorieAnalyst Assistant Commented:
How would the datetime pickers be populated based on what's selected in the second combobox modelcombo?
Gökmen Anıl YılmazStudentAuthor Commented:
Could you suggest another solution because I do not know  by: Geert G
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Gökmen Anıl YılmazStudentAuthor Commented:
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
NorieAnalyst Assistant Commented:
Will the second combobox be populated with multiple values?

If so, what exactly do you want to show in the datetime pickers?
Gökmen Anıl YılmazStudentAuthor Commented:
1. combobox 10 values and 2. combobox i think 50 values.

model production and end date
NorieAnalyst Assistant Commented:
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.
Gökmen Anıl YılmazStudentAuthor Commented:
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
NorieAnalyst Assistant Commented:
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?
Gökmen Anıl YılmazStudentAuthor Commented:
Yes that's right
Gökmen Anıl YılmazStudentAuthor Commented:
but how can i :)
NorieAnalyst Assistant Commented:
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

Gökmen Anıl YılmazStudentAuthor Commented:
i know but i do not know

this code

I wrote here my fault

            ' code to set values of datetime pickers.

can you say
NorieAnalyst Assistant Commented:
Try this.
        private void modelcombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataAdapter sda = new SqlDataAdapter("select productiondate, enddate from tbl_markamodel where marka='" + markacombo.Text + "' AND model='" + modelcombo.Text + "'", conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            DataRow dates = dt.Rows[0];
            
            DTProdDate.Value = DateTime.Parse(dates[0].ToString());
            DTEndDate.Value = DateTime.Parse(dates[1].ToString());
        }

Open in new window


Note I've changed the name of some things, and added the names for some other things, i.e. the datetime pickers.

In the original code the database connection was named baglanti, I've named it conn in the code I posted.

In the table I've renamed the columns/fields URETIMTARIHI and BITISTARIHI to ProductionDate and EndDate respectively.

On the form I've named the datetime pickers DTProdDate and DTEndDate.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gökmen Anıl YılmazStudentAuthor Commented:
Thank for help and answers
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.