Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:11:55+00:00 2026-05-31T17:11:55+00:00

I have a dataset that returns questions and answers from the database, each answer

  • 0

I have a dataset that returns questions and answers from the database, each answer in answer table is linked via a forgein key to the question table.

What I would like to achive is the following:

That a a single dynamic form is created on the first question with all the questions listed under it, meaning the FK_table_Answers = PK_table_Questions on the table_Answers:

(eg) This is Form for Question One:

    Question One: How old are you?

   Answer One (this is a radio button)

   Answer Two (this is a radio button)

  Answer Three (this is a radio button)

I have managed to get the above kind of working but the problem is my code keeps generating the forms (eg) 3 times becuase there are three answers, so can anyone point me in right direction how to generate the form dynamically but only ONCE – meaning each successive form is created only once as per the question and that particular questions answers under it.

Kind regards

UPDATED code:

    private void LoadDataSets()
    {
        if (_dataSetQuestionnaire.tbl_QuestionnaireQuestion.Rows.Count == 0)
        {
            try
            {
                //Questionnaire:
                DataSet1TableAdapters.tbl_QuestionnaireTableAdapter questionnaireAdapter =
                                 new DataSet1TableAdapters.tbl_QuestionnaireTableAdapter();
                questionnaireAdapter.Fill(_dataSetQuestionnaire.tbl_Questionnaire);

                //Category:
                DataSet1TableAdapters.tbl_QuestionnaireCategoryTableAdapter categoryAdapter =
                               new DataSet1TableAdapters.tbl_QuestionnaireCategoryTableAdapter();
                categoryAdapter.Fill(_dataSetQuestionnaire.tbl_QuestionnaireCategory);

                //QuestionnaireQuestion:
                DataSet1TableAdapters.tbl_QuestionnaireQuestionTableAdapter questionnaireQuestionAdapter =
                                  new DataSet1TableAdapters.tbl_QuestionnaireQuestionTableAdapter();
                questionnaireQuestionAdapter.Fill(_dataSetQuestionnaire.tbl_QuestionnaireQuestion);

                //QuestionnaieAnswer:
                DataSet1TableAdapters.tbl_QuestionnaireAnswerTableAdapter questionnaireAnswerAdapter =
                                 new DataSet1TableAdapters.tbl_QuestionnaireAnswerTableAdapter();
                questionnaireAnswerAdapter.Fill(_dataSetQuestionnaire.tbl_QuestionnaireAnswer);

                using (DataSet1 dSet = new DataSet1())
                {
                    //Questionnaire:
                    dSet.Merge(_dataSetQuestionnaire.tbl_Questionnaire);
                    //Category:
                    dSet.Merge(_dataSetQuestionnaire.tbl_QuestionnaireCategory);
                    //QuestionnaireQuestion:
                    dSet.Merge(_dataSetQuestionnaire.tbl_QuestionnaireQuestion);
                    //QuestionnaieAnswer:
                    dSet.Merge(_dataSetQuestionnaire.tbl_QuestionnaireAnswer);

                    int primaryKeyQuestionnaire = Convert.ToInt32(_dataSetQuestionnaire.tbl_Questionnaire.Rows[0][0]);

                    foreach (DataSet1.tbl_QuestionnaireRow questionnaire
                                        in _dataSetQuestionnaire.Tables["tbl_Questionnaire"].Select(String.Format("pk_tbl_Questionnaire = {0}", primaryKeyQuestionnaire)))
                    {
                        foreach (DataSet1.tbl_QuestionnaireCategoryRow category
                                       in _dataSetQuestionnaire.Tables["tbl_QuestionnaireCategory"].Select(String.Format("fk_tbl_Questionnaire = {0}", questionnaire.pk_tbl_Questionnaire)))

                                       {
                            foreach (DataSet1.tbl_QuestionnaireQuestionRow question
                                           in _dataSetQuestionnaire.Tables["tbl_QuestionnaireQuestion"].Select(String.Format("fk_tbl_QuestionnaireCategory = {0}", category.pk_tbl_QuestionnaireCategory)))
                            {
                                int radiobuttonPosition = 0;
                                foreach (DataSet1.tbl_QuestionnaireAnswerRow answer
                                                   in _dataSetQuestionnaire.Tables["tbl_QuestionnaireAnswer"].Select(String.Format("fk_tbl_QuestionnaireQuestion = {0}", question.pk_tbl_QuestionnaireQuestion)))
                                {


                                    //Gets the questins via the FK_questionnaireQuestion and fill the _dataSetRadioButtons to generate on dynamic form.
                                    DataSet1TableAdapters.tbl_QuestionnaireAnswerTableAdapter a =
                                                     new DataSet1TableAdapters.tbl_QuestionnaireAnswerTableAdapter();

                                    DataSet dSetRadioButtons = new DataSet();
                                    dSetRadioButtons.Merge(a.GetDataByQuestionnaireQuestion(answer.fk_tbl_QuestionnaireQuestion));
                                    _dataSetRadioButtons = dSetRadioButtons;

                                    string theQuestion = question.tbl_QuestionnaireQuestion_Description.ToString();

                                    Form form = new Form();
                                    _form = form;

                                    if(_form == null)
                                    {
                                        _form = new Form();
                                    }
                                    else
                                    {
                                        form.Height = 400;
                                        form.Width = 550;
                                        form.StartPosition = FormStartPosition.CenterScreen;

                                        Label label = new Label();
                                        label.Location = new System.Drawing.Point(5, 10);
                                        label.Size = new System.Drawing.Size(450, 25);
                                        label.Text = theQuestion;

                                        Panel panel = new Panel();
                                        panel.Size = new System.Drawing.Size(300, 200);
                                        panel.Location = new System.Drawing.Point(15, 50);
                                        panel.BackColor = Color.Yellow;

                                        System.Windows.Forms.RadioButton[] radioButtons = new System.Windows.Forms.RadioButton[_dataSetRadioButtons.Tables["tbl_QuestionnaireAnswer"].Rows.Count];

                                        for (int i = 0; i < _dataSetRadioButtons.Tables["tbl_QuestionnaireAnswer"].Rows.Count; i++)
                                        {
                                            radioButtons[i] = new RadioButton();
                                            radioButtons[i].Text = _dataSetRadioButtons.Tables["tbl_QuestionnaireAnswer"].Rows[i]["tbl_QuestionnaireAnswer_Description"].ToString();
                                            radioButtons[i].Location = new System.Drawing.Point(60, 20 + i * 20);

                                            //panel.Controls.Add(radioButtons[i]);

                                            radioButtons[i].Click += new EventHandler(Form1_Click);

                                            Int64 item = Convert.ToInt64(_dataSetRadioButtons.Tables["tbl_QuestionnaireAnswer"].Rows[0].ItemArray[3].ToString());
                                            panel.Controls.Add(radioButtons[i]);
                                        }

                                        Button nextButton = new Button();
                                        nextButton.Text = "Next";
                                        nextButton.Name = "button";
                                        nextButton.Location = new System.Drawing.Point(200, 300);
                                        nextButton.Size = new System.Drawing.Size(150, 25);
                                        nextButton.Click += new EventHandler(nextButton_Click);

                                        form.Controls.AddRange(new Control[] { panel, label, nextButton });

                                        form.ShowDialog();

                                        CreateBoxAndQuestion(form, panel, label);

                                        //form.Dispose();
                                    }

                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        else
        {
            MessageBox.Show("Error");
        }
    }

    private void CreateBoxAndQuestion(Form f, Panel p, Label l)  
    {
        for (int i = p.Controls.Count - 1; i >= 0; i--)
        {
            p.Controls.RemoveAt(i);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:11:56+00:00Added an answer on May 31, 2026 at 5:11 pm

    If I understand correctly, you call CreateBoxAndQuestion every time a new question is selected.
    But you create (and destroy) the form each time your code enters in this method.
    A first approach would be to keep the form, label, button and panel creation outside the method (perhaps in design mode) then passing these controls to the method as

    EDIT: Somewhere in your code before entering the load of your next question:

    frmQuestion _form = null; // Global
    
    // Create a global instance and keep it without displaying
    if(_form == null) _form = new frmQuestion(); // frmQuestion created with panel, label e button via FormDesigner
    

    then when you need to populate _form call

    CreateBoxAndQuestion(_form, _form.Panel, _form.Label, questionText);
    

    In this example I pass the _form to CreateBoxAndQuestion, but this is not necessary because is a global. You could change CreateBoxAndQuestion to use directly the global instance.

    private void CreateBoxAndQuestion(frmQuestion f, Panel p, Label l, string _label) 
    {
        // Do not display your form here....
    }
    

    Now when entering the method clear every RadioButton controls in the panel.Controls collection

    for (int i = p.Controls.Count - 1; i >= 0; i--) 
    { 
        p.Controls.RemoveAt(i); 
    } 
    

    the rest of the code should change only to reflect the new text assigned to the label and controls and the re-adding of the RadioButtons to the panel. No more creation and reinitialization for Form Label, Button.

    label.Text = _label;

    Don’t forget to destroy _form at the end of your program with _form.Dispose();

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that returns a table (or dataset or view or
I have a web service which has a generic function that returns a dataset
I have a dataset that points to a View in my SQL database, and
I have a DataSet with some DataTables that are linked together with DataRelations (classic
I have a DataSet which I get a DataTable from that I am being
I have a GridView that is bound to a select statement from a table.
I have a dataset that I have modified into an xml document and then
I have a dataset that has two tables in it. I want to do
I have a DataSet that contains a few columns. One of these columns is
Imagine you have a large dataset that may or may not be filtered by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.