CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Join Date
    Feb 2012
    Posts
    46

    [RESOLVED] iteration from the database

    Code:
     private void rolePanel_Click(object sender, EventArgs e)
            {
                string DBConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\LinksApplication.mdf;Integrated Security=True;User Instance=True";
                SqlConnection sqlConnection = new SqlConnection(DBConnection);
                CheckBox roleCheck = new CheckBox();
                //Panel rolePanel = new Panel();
                try
                {
                    sqlConnection.Open();
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();
                    sqlCommand.CommandType = System.Data.CommandType.Text;
                    sqlCommand.CommandText = "SELECT * FROM Role";
                    /*SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                    DataSet dataSet = new DataSet();
                    sqlDataAdapter.Fill(dataSet, "Role");
                    DataTable dataTable=dataSet.Tables["Role"];
                    foreach(DataRow dataRow in dataTable.Rows){
                    new CheckBox().Text=dataRow["role"].ToString();
                    rolePanel.Controls.Add(new CheckBox());
                    rolePanel.Visible = true;*/
                    
                    SqlDataReader sqlDatareader = sqlCommand.ExecuteReader();
    
                    //CREATE AN INSTANCE OF A CHECKBOX AND SET ATTRIBUTES
                    roleCheck.TextAlign = ContentAlignment.MiddleRight;
    
                    while (sqlDatareader.Read())
                    {
                        roleCheck.Text = sqlDatareader["role"].ToString();
                        rolePanel.Controls.Add(roleCheck);
                        rolePanel.Visible = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
    PLEASE KINDLY HELP ME OUT. I WANT TO ITERATE THE VALUES FROM THE DATABASE TABLE INTO A PANEL(rolePanel) AND ADDING CHECKBOX TO EACH ITEM, BUT FOR NOW IT ONLY POPULATES IT WITH JUST ONE ITEM FROM THE DATABASE TABLE. I THINK MY ITERATE STATEMENT ISN'T RIGHT, WHAT SHOULD I DO? THANKS. GOD BLESS IN JESUS NAME.

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: iteration from the database

    There is a lot of your code which is done between /* ....*/ so this only makes your request a bit unreadable Basically if you want to add Controls in each iteration you need to create a new one. Thats your main outpoint in your code, Additional you should be able to check your code to
    a) is it ok so it could be comppiled if not remove all error so it can be compiled
    b) Debug your code running it line by liine so you see if your items are read from the database and if your controls are created and correctly added to your panel

    In the moment this seems me both to be a problem
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Feb 2012
    Posts
    46

    Re: iteration from the database

    A big thanks for your observation. I appreciate this.

    Code:
     private void personPanel_Click(object sender, EventArgs e)
            {
                string DBConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\LinksApplication.mdf;Integrated Security=True;User Instance=True";
                SqlConnection sqlConnection = new SqlConnection(DBConnection);
                sqlConnection.Open();
                try{
                    
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();
                    sqlCommand.CommandText = "SELECT * FROM Person";
                    sqlCommand.CommandType = System.Data.CommandType.Text;
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                    DataSet dataSet = new DataSet();
                    sqlDataAdapter.Fill(dataSet, "Person");
                    DataTable dataTable=dataSet.Tables["Person"];
    
                    foreach(DataRow dataRow in dataTable.Rows){
                        int rowsPerson = dataSet.Tables["Person"].Rows.Count;
                        string strFirtsname=dataRow["firstname"].ToString();
                        string strLastname=dataRow["lastname"].ToString();
    
                        for (int count = 0; count < rowsPerson; count++)
                        {
                            RadioButton[] personRadio = new RadioButton[rowsPerson];
                            personRadio[count] = new RadioButton();
                            personRadio[count].Name = personRadio[count].ToString();
                            MessageBox.Show("No. of Rows are : " + personRadio[count].ToString());
                            personRadio[count].Text = strFirtsname + " " + strLastname;
                            // MessageBox.Show("No. of Rows are : " + personRadio[count].Text.ToString());
                            personRadio[count].TabIndex = count;
                            personRadio[count].Location = new Point(0, count * 20);
                            personRadio[count].AutoCheck = true;
                            personPanel.Controls.Add(personRadio[count]);
                            personPanel.Visible = true;
                        }
                    }
    
                
                }
                catch(Exception ex){
                    MessageBox.Show(ex.ToString());
                }
                finally{
                
                }
            }
    I have added more codes to it but now it is iterating the same Roll according to the number of Roles in the table. For example let me say if I have 3 roles in a table, it assigns 3 radioButtons to only a single row(in the table)

    ie let say I have the following in my table
    1. jon
    2. fred
    3.kehinde

    it iterates 9 times and assigns radio button three times to the first role alone as follows:

    (radioButton) jon
    (radioButton) jon
    (radioButton) jon

    what should I do? thanks.

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: iteration from the database

    Code:
     private void personPanel_Click(object sender, EventArgs e)
            {
                string DBConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\LinksApplication.mdf;Integrated Security=True;User Instance=True";
                SqlConnection sqlConnection = new SqlConnection(DBConnection);
                sqlConnection.Open();
                try{
     
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();
                    sqlCommand.CommandText = "SELECT * FROM Person";
                    sqlCommand.CommandType = System.Data.CommandType.Text;
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                    DataSet dataSet = new DataSet();
                    sqlDataAdapter.Fill(dataSet, "Person");
                    DataTable dataTable=dataSet.Tables["Person"];
                    int count = 0;; //Adsd this
                   // The following needs to be placed outside the loop
                   int rowsPerson = dataSet.Tables["Person"].Rows.Count;
                   RadioButton[] personRadio = new RadioButton[rowsPerson];               
     foreach(DataRow dataRow in dataTable.Rows){
                        
                        string strFirtsname=dataRow["firstname"].ToString();
                        string strLastname=dataRow["lastname"].ToString();
     
                      //  for (int count = 0; count < rowsPerson; count++)
                       // {  Why would you have a loop iin a loop
                            RadioButton[] personRadio = new RadioButton[rowsPerson]; // Thiis needs to be outside the loop because otherwise you would create a new aeeay each time you arer lpoping through that 
                            personRadio[count] = new RadioButton();
                            personRadio[count].Name = personRadio[count].ToString();
                            MessageBox.Show("No. of Rows are : " + personRadio[count].ToString());
                            personRadio[count].Text = strFirtsname + " " + strLastname;
                            // MessageBox.Show("No. of Rows are : " + personRadio[count].Text.ToString());
                            personRadio[count].TabIndex = count;
                            personRadio[count].Location = new Point(20, count * 20);
                            personRadio[count].AutoCheck = true;
                            personPanel.Controls.Add(personRadio[count]);
                            personPanel.Visible = true;
                           count ++; // Add this
                       // } only one loop so no longer needed
                    }
     
     
                }
                catch(Exception ex){
                    MessageBox.Show(ex.ToString());
                }
                finally{
     
                }
            }
    As already mentione any array in that nneds to be outside any loop.. Otherwise you will get a lot of problems The array would only life as long as you are inside the loop so a new array would be created each time.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Feb 2012
    Posts
    46

    Re: iteration from the database

    Just want to show my gratitude for your great help. It works perfectly. Thanks. God bless in Jesus name.

  6. #6
    Join Date
    Feb 2012
    Posts
    46

    Re: iteration from the database

    Code:
    private void rolePanel_Click(object sender, EventArgs e)
            {
                string DBConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\LinksApplication.mdf;Integrated Security=True;User Instance=True";
                SqlConnection sqlConnection = new SqlConnection(DBConnection);
                CheckBox roleCheck = new CheckBox();
    
                try
                {
                    sqlConnection.Open();
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();
                    sqlCommand.CommandType = System.Data.CommandType.Text;
                    sqlCommand.CommandText = "SELECT * FROM Role";
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                    DataSet dataSet = new DataSet();
                    sqlDataAdapter.Fill(dataSet, "Role");
                    DataTable dataTable = dataSet.Tables["Role"];
                    int rowRole = dataSet.Tables["Role"].Rows.Count;
    
                    int count = 0;
                    CheckBox[] checkRole = new CheckBox[rowRole];
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        string strRole = dataRow["role"].ToString();
                        checkRole[count] = new CheckBox();
                        checkRole[count].Name = checkRole[count].ToString();
                        checkRole[count].Text = strRole;
                        checkRole[count].TabIndex = count;
                        checkRole[count].Location = new Point(20, count * 20);
                        checkRole[count].AutoCheck = true;
                        rolePanel.Controls.Add(checkRole[count]);
                        rolePanel.Visible = true;
                        count++;
    
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
    this is working properly. The next thing I want to do is to get the selected radio button. Your contribution is highly appreciated.

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: iteration from the database

    Code:
    private void rolePanel_Click(object sender, EventArgs e)
            {
                string DBConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\LinksApplication.mdf;Integrated Security=True;User Instance=True";
                SqlConnection sqlConnection = new SqlConnection(DBConnection);
                CheckBox roleCheck = new CheckBox();
     
                try
                {
                    sqlConnection.Open();
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();
                    sqlCommand.CommandType = System.Data.CommandType.Text;
                    sqlCommand.CommandText = "SELECT * FROM Role";
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                    DataSet dataSet = new DataSet();
                    sqlDataAdapter.Fill(dataSet, "Role");
                    DataTable dataTable = dataSet.Tables["Role"];
                    int rowRole = dataSet.Tables["Role"].Rows.Count;
     
                    int count = 0;
                    CheckBox[] checkRole = new CheckBox[rowRole];
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        string strRole = dataRow["role"].ToString();
                        checkRole[count] = new CheckBox();
                        checkRole[count].Name = checkRole[count].ToString();
                        checkRole[count].Text = strRole;
                        checkRole[count].TabIndex = count;
                        checkRole[count].Location = new Point(20, count * 20);
                        checkRole[count].AutoCheck = true;
                       // Add a delegate here to the control so you are able to get clicks to thast checkboxes
                        checkRole[count].Click += new EventHandler(RolePanel_Click);                    rolePanel.Controls.Add(checkRole[count]);
                        rolePanel.Visible = true;
                        count++;
     
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
           // Add the e delegate 
            void RolePanel_Click(object sender, EventArgs e) {
                CheckBox actBox = (CheckBox)sender;
                int count = actBox.TabIndex;
            
            }
    Last edited by JonnyPoet; March 6th, 2012 at 11:36 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Feb 2012
    Posts
    46

    Re: iteration from the database

    This language is a bit advance than my level of knowledge in C# now, I am only 3wks old in this, please can you give me code that relates to this( Add a delegate here to the control so you are able to get clicks to that checkboxes). Thanks.

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: iteration from the database

    I added the code its the line after my red text and the delegate is done in the bottom of the code. I have colored the code to blue lines now.
    Last edited by JonnyPoet; March 6th, 2012 at 11:40 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  10. #10
    Join Date
    Feb 2012
    Posts
    46

    Re: iteration from the database

    I can not but tell you that my appreciation goes beyond writing. Thanks a lot!

  11. #11
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: iteration from the database

    Quote Originally Posted by ken4ward View Post
    I can not but tell you that my appreciation goes beyond writing. Thanks a lot!
    You are welcome
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  12. #12
    Join Date
    Feb 2012
    Posts
    46

    Re: [RESOLVED] iteration from the database

    Code:
    int rowsPerson = dataSet.Tables["Person"].Rows.Count;
    
                    int count = 0;
                    RadioButton[] personRadio = new RadioButton[rowsPerson];
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
    
                        string strFirtsname = dataRow["firstname"].ToString();
                        string strLastname = dataRow["lastname"].ToString();
                        personRadio[count].personRadio_CheckedChanged = new EventHandler(personRadio_CheckChanged);
                        personRadio[count] = new RadioButton();
                        personRadio[count].Name = personRadio[count].ToString();
                        personRadio[count].Text = strFirtsname + " " + strLastname;
                        personRadio[count].TabIndex = count;
                        personRadio[count].Location = new Point(20, count * 20);
                        personRadio[count].AutoCheck = true;
                        personPanel.Controls.Add(personRadio[count]);
                        personPanel.Visible = true;
                        count++;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
            private void personRadio_CheckChanged(object sender, System.EventArgs e)
            {
                RadioButton personRadio=(RadioButton)sender;
                int count = personRadio.TabIndex;
            }
    Can not explain why the CheckChanged eventHandler is not working. Please, I need your insight. Thanks.

  13. #13
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: [RESOLVED] iteration from the database

    Code:
    int rowsPerson = dataSet.Tables["Person"].Rows.Count;
     
                    int count = 0;
                    RadioButton[] personRadio = new RadioButton[rowsPerson];
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
     
                        string strFirtsname = dataRow["firstname"].ToString();
                        string strLastname = dataRow["lastname"].ToString();
                       // This even will not compile I think !!!  Because tzhere is no Event called personRadio_CheckedChanged                  
                       personRadio[count].personRadio_CheckedChanged = new EventHandler(personRadio_CheckChanged);
                      //THIS NEEDS TO BE
                       personRadio[count].CheckedChanged = new EventHandler(personRadio_CheckChanged);
                        personRadio[count] = new RadioButton();
                        personRadio[count].Name = personRadio[count].ToString();
                        personRadio[count].Text = strFirtsname + " " + strLastname;
                        personRadio[count].TabIndex = count;
                        personRadio[count].Location = new Point(20, count * 20);
                        personRadio[count].AutoCheck = true;
                        personPanel.Controls.Add(personRadio[count]);
                        personPanel.Visible = true;
                        count++;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
            private void personRadio_CheckChanged(object sender, System.EventArgs e)
            {
                RadioButton personRadio=(RadioButton)sender;
                int count = personRadio.TabIndex;
            }
    Last edited by JonnyPoet; March 8th, 2012 at 01:57 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  14. #14
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: [RESOLVED] iteration from the database

    OK As I can see you did not get the idea behind all of that. So I'll explain that to you.
    Each Control may have Properties, methods and and events
    Using the intellisense will show you all of them
    This way, when you type
    personRadio[count]. After typing the dot the intellisense will open and show you all of them. No you easily can see what events the control provides.

    Now click to that event you want to use e.g
    personRadio[count].CheckedChanged
    and type += after you have choosen the event you want to use.
    The code now will be
    personRadio[count].CheckedChanged +=

    Now two times press the TAB key of your keyboard and you will automatically get something like the following
    Code:
    personRadio[count].CheckedChanged = new EventHandler(personRadio_CheckChanged);
    You can see that as well the binding of the delegate to the event is done on the one side and additional the methodstub of that delegate is created
    Code:
      private void personRadio_CheckChanged(object sender, System.EventArgs e)
            {
                      
            }
    Last edited by JonnyPoet; March 8th, 2012 at 02:25 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  15. #15
    Join Date
    Feb 2012
    Posts
    46

    Re: [RESOLVED] iteration from the database

    Code:
     public void personPanel_Click(object sender, EventArgs e)
            {
                string DBConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\LinksApplication.mdf;Integrated Security=True;User Instance=True";
                SqlConnection sqlConnection = new SqlConnection(DBConnection);
                sqlConnection.Open();
                try
                {
    
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();
                    sqlCommand.CommandText = "SELECT * FROM Person";
                    sqlCommand.CommandType = System.Data.CommandType.Text;
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                    DataSet dataSet = new DataSet();
                    sqlDataAdapter.Fill(dataSet, "Person");
                    DataTable dataTable = dataSet.Tables["Person"];
                    int rowsPerson = dataSet.Tables["Person"].Rows.Count;
    
                    int count = 0;
                    RadioButton[] personRadio = new RadioButton[rowsPerson];
                    
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
    
                        string strFirtsname = dataRow["firstname"].ToString();
                        string strLastname = dataRow["lastname"].ToString();
                        personRadio[count] = new RadioButton();
                        personRadio[count].Name = personRadio[count].ToString(); 
                        personRadio[count].Text = strFirtsname + " " + strLastname;
                        personRadio[count].TabIndex = count;
                        personRadio[count].Location = new Point(20, count * 20);
                        personRadio[count].AutoCheck = true;
                        personRadio[count].CheckedChanged += new EventHandler(personRadio_CheckChanged);
                        personPanel.Controls.Add(personRadio[count]);
                        personPanel.Visible = true;
                        count++;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
            private void personRadio_CheckChanged(object sender, System.EventArgs e) {
                RadioButton personRadio = (RadioButton)sender;
            }
    
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    private void circumstancePanel_Click(object sender, EventArgs e)
            {
                string DBConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\LinksApplication.mdf;Integrated Security=True;User Instance=True";
                SqlConnection sqlConnection = new SqlConnection(DBConnection);
    
                try
                {
                    sqlConnection.Open();
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();
                    sqlCommand.CommandType = System.Data.CommandType.Text;
                    sqlCommand.CommandText = "SELECT * FROM Circumstance";
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                    DataSet dataSet = new DataSet();
                    sqlDataAdapter.Fill(dataSet, "Circumstance");
                    DataTable dataTable = dataSet.Tables["Circumstance"];
                    int rowCircumstance = dataSet.Tables["Circumstance"].Rows.Count;
    
                    int count = 0;
                    CheckBox[] checkCircumstance = new CheckBox[rowCircumstance];
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        string strRole = dataRow["circumstance"].ToString();
                        checkCircumstance[count] = new CheckBox();
                        checkCircumstance[count].Name = checkCircumstance[count].ToString();
                        checkCircumstance[count].Text = strRole;
                        checkCircumstance[count].TabIndex = count;
                        checkCircumstance[count].Location = new Point(20, count * 20);
                        checkCircumstance[count].AutoCheck = true;
                        circumstancePanel.Controls.Add(checkCircumstance[count]);
                        circumstancePanel.Visible = true;
                        count++;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
    
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
     private void linkUser_Click(object sender, EventArgs e)
            {
                if(personPanel.Controls!=null){
                foreach(Control items in personPanel.Controls){
                
                }
                    if(circumstancePanel.Controls!=null){
                    foreach(Controls .........//How do i write the code for the selected checkbox(es) ){
                    
                    }
    Hello Jonny, your contribution to the success of this my project is really applauding. i heartily say many thanks!

    Explain in bits what I want to achieve in this line of codes:
    personPanel contains dynamically added radio buttons based on the number of items in the database table
    circumstancePanel contains dynamically added checkboxes based on the number of items in the database table
    Now I want to use linkUser_Click(sender, e) to achieve this:
    When a personRadio (radio Button) is clicked on the interface of personPanel and many other checkboxes are checked in circumstancePanel i want it to work this way:
    Anytime that the same radio button in personPanel interface is clicked it should display the checked checkbox as its assigned values. I have a panel I want to add this to already.
    My immense appreciation goes beyond writing. Thanks. Gold bless in Jesus name.
    Last edited by ken4ward; March 12th, 2012 at 05:54 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured