Home » MS OfficeRSS

How to divide this code?

Hi I have code wich biding data to combobox.

I have two combobox.with two colomuns.PK and FK now I need to da that IF I select 1000 in combobox corporationcode1 in combobox workspacecode I need to display only value wich beginns in 1000/18 this code do that but I need to divide this code in step step one populate data to first combobox after than go to second combobox and populate the match data in second combobox.

a problem if I run debug on my code the data populated not by step all combobox. 

privatevoid Window_Loaded(object sender, RoutedEventArgs e)

       {

                DataSet ds2 = newDataSet();

                SqlConnection con2 = newSqlConnection("Data Source=D;Initial Catalog=DMS;Integrated Security=True");

                SqlCommand cmd2 = newSqlCommand("SELECT CUSTOM_ALIAS, C_DESCRIPT FROM [MHGROUP].CUSTOM1", con2);

                SqlDataAdapter sqlDa2 = newSqlDataAdapter();

                sqlDa2.SelectCommand = cmd2;

                sqlDa2.Fill(ds2);

 

 

                CorporationName.ItemsSource = ds2.Tables[0].DefaultView;

                CorporationCode1.ItemsSource = ds2.Tables[0].DefaultView;

 

 

                CorporationCode1.SelectedIndex = -1;

 

          }

 

 

          

                            {

               

 

                    var value1 = CorporationCode1.Text;

                    DataSet ds2 = newDataSet();

                    SqlConnection con2 = newSqlConnection("Data Source=D;Initial Catalog=DMS;Integrated Security=True");

                    //SqlCommand cmd2 = new SqlCommand("SELECT * FROM [MHGROUP].CUSTOM2 WHERE CPARENT_ALIAS ='" + value1 + "'", con2);

                    SqlCommand cmd2 = newSqlCommand("SELECT * FROM [MHGROUP].CUSTOM2 WHERE SUBSTRING (CPARENT_ALIAS  ,1 ,1000) = 'value1'", con2);

                    SqlDataAdapter sqlDa2 = newSqlDataAdapter();

                    sqlDa2.SelectCommand = cmd2;

                    sqlDa2.Fill(ds2);

 

 

                    WorkspaceCode.ItemsSource = ds2.Tables[0].DefaultView;

                    WorkspaceName.ItemsSource = ds2.Tables[0].DefaultView;

                    WorkspaceName.SelectedIndex = -1;

                } 

        }

thank you

 

4 Answers Found

 

Answer 1

Hi DenisSL1987,

To be honest, I do not fully understand your question. Would you please kindly elaborate your question again?

Thank you for your understanding!

Best regards,

Min

 

Answer 2

Hi Min.

I bind data  from sql tables to comboboxes I have three comboboxes.

1 .Division code  .Value in sql tb{1000,2000,3000,}

2.Corporation Code. {1001,1002,2000,2001,3001,3002 etc...}

3.WorkspaceCode.{1001/1,1001/2,2000/1,2001/3 etc..}

between Sql tables CorproationCode and WorkspaceCode I have connection PK,FK in Column CPARENT_ALIAS

My task is to display data to User from DB.

if I chose in CorporationCode combobox  value = 1001 in combobox WorkspaceCode I need to display only values which begin with 1001/1 ...1001/2 etc..

now I have code wich  bind data to comboboxes but in the start the UI windows data bind automaticly to all comboboxes my tak to bind data by step combobox 1 and 2 after 3 in combobox data biding  must be do on rely data was selected in combobox 2.

There is code :

 

        privatevoid Window_Loaded(object sender, RoutedEventArgs e)

        {

 

       

           

            {

 

                DataSet ds1 = newDataSet();

                SqlConnection con = newSqlConnection("Data Source=DYS;Initial Catalog=DMS;Integrated Security=True");

                SqlCommand cmd1 = newSqlCommand("SELECT CUSTOM_ALIAS, C_DESCRIPT FROM [MHGROUP].CUSTOM3", con);

                SqlDataAdapter sqlDa1 = newSqlDataAdapter();

                sqlDa1.SelectCommand = cmd1;

                sqlDa1.Fill(ds1);

 

 

 

                DivisonName.ItemsSource = ds1.Tables[0].DefaultView;

                DivisionCode.ItemsSource = ds1.Tables[0].DefaultView;

                DivisionCode.SelectedIndex = -1;

          }

           

       

       

 

         

 

            {

                //if (DivisionCode.SelectedIndex != -1)

 

 

                DataSet ds2 = newDataSet();

                SqlConnection con2 = newSqlConnection("Data Source=DYS;Initial Catalog=DMS;Integrated Security=True");

                SqlCommand cmd2 = newSqlCommand("SELECT CUSTOM_ALIAS, C_DESCRIPT FROM [MHGROUP].CUSTOM1", con2);

                SqlDataAdapter sqlDa2 = newSqlDataAdapter();

                sqlDa2.SelectCommand = cmd2;

                sqlDa2.Fill(ds2);

 

 

                CorporationName.ItemsSource = ds2.Tables[0].DefaultView;

                CorporationCode1.ItemsSource = ds2.Tables[0].DefaultView;

 

 

                CorporationCode1.SelectedIndex = -1;

            }

       

       

      

 

            

            {

 

 

 

                var value1 = CorporationCode1.Text;

                // MessageBox.Show(value1);

                DataSet ds3 = newDataSet();

                SqlConnection con3 = newSqlConnection("Data Source=DYS;Initial Catalog=DMS;Integrated Security=True");

                SqlCommand cmd3 = newSqlCommand("SELECT * FROM [MHGROUP].CUSTOM2 WHERE SUBSTRING (CPARENT_ALIAS  ,1 ,4) = '" + ((value1)) + "'", con3);

                SqlDataAdapter sqlDa3 = newSqlDataAdapter();

                sqlDa3.SelectCommand = cmd3;

                sqlDa3.Fill(ds3);

 

 

                WorkspaceCode.ItemsSource = ds3.Tables[0].DefaultView;

                WorkspaceName.ItemsSource = ds3.Tables[0].DefaultView;

                WorkspaceName.SelectedIndex = -1;

            }

       

        }

 

 

 

Answer 3

Hi DenisSL1982,

I think you can add an eventhandler for CorporationCode1.SelectionChanged event and place the third posted code block into that eventhandler. In this way, the third combobox  will update when something is selected in the second combobox.

Hope this helps.

Best regards,

Min

 

Answer 4

Thank you for reply.

I already do this code  with adding slectedevents.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter