none
How to divide this code?

    Question

  • 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. 

    private void Window_Loaded(object sender, RoutedEventArgs e)

           {

                    DataSet ds2 = new DataSet();

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

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

                    SqlDataAdapter sqlDa2 = new SqlDataAdapter();

                    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 = new DataSet();

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

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

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

                        SqlDataAdapter sqlDa2 = new SqlDataAdapter();

                        sqlDa2.SelectCommand = cmd2;

                        sqlDa2.Fill(ds2);

     

     

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

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

                        WorkspaceName.SelectedIndex = -1;

                    } 

            }

    thank you

    Monday, September 20, 2010 11:13 AM

Answers

  • 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


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Dan_1982 Monday, September 27, 2010 8:30 AM
    Monday, September 27, 2010 4:59 AM
    Moderator

All replies

  • 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


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, September 23, 2010 8:49 AM
    Moderator
  • 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 :

     

            private void Window_Loaded(object sender, RoutedEventArgs e)

            {

     

           

               

                {

     

                    DataSet ds1 = new DataSet();

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

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

                    SqlDataAdapter sqlDa1 = new SqlDataAdapter();

                    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 = new DataSet();

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

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

                    SqlDataAdapter sqlDa2 = new SqlDataAdapter();

                    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 = new DataSet();

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

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

                    SqlDataAdapter sqlDa3 = new SqlDataAdapter();

                    sqlDa3.SelectCommand = cmd3;

                    sqlDa3.Fill(ds3);

     

     

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

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

                    WorkspaceName.SelectedIndex = -1;

                }

           

            }

     

     

    Thursday, September 23, 2010 9:54 AM
  • 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


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Dan_1982 Monday, September 27, 2010 8:30 AM
    Monday, September 27, 2010 4:59 AM
    Moderator
  • Thank you for reply.

    I already do this code with adding slectedevents.

    Monday, September 27, 2010 8:30 AM