Insert into one to many relationship

Posted on 2007-07-30
Last Modified: 2013-11-26
I want to create an asp page for one to many relationship. I have questions in one table and possible answers in other table. Now when i add a new question at the same time i have to add the possible answers. I managed to add a new question using a form and after its added; a button click takes to a new page to add answers. What the best way to add possible asnwers ? How shall i display my form and which control will be good for it ? (Also a question might have 2 to 5 possible Answers) My possibleAnswer table has three fields QuestionID,PossibleAnswer,CorrectAnswer
using VS2005 with & SQL server 2005
Thanks in advance
Question by:mmalik15
    LVL 7

    Expert Comment

    I didn't really understood your question, but i will try to answer as i understood it,

    What the best way to add possible answers?
    --Okey, i take it possible answers will be a foreign key in your questions table and might have constraints so it won't accept  null value....

    in that case you might have default possible answers for example:

    Answers Table:

    AnswersID Answer
    0                  0
    1                 Blue
    3                 Red

    Questions Table:

    QuestionID PossibleAnswer CorrectAnswer
    1                 0                           0
    2                 1                           3

    How shall i display my form and which control will be good for it ?

    You can use formview to insert/update/select (single row) to the DB, but if you just want to display multiple records then i would recommend to use Gridviews....

    Hope that helps


    Author Comment

    Thanks for your comment. I will try to explain it bit more.
    I have two tables Question(QuestionID,QuestionDesc, NoOfPossibleAnswers,Status) and QuestionPossibleAnswers(QuestionID,PossibleAnswer,Answers).  A parent form where i add a question (which i manage to add) and now i have to add the Question possible answers (which can be many). Now how the child form can be designed by keeping in view i have to add a specific no of possible answers ? if i use a formview control in the insert mode after a record is inserted what shud happen ? i am bit confused about this
    LVL 7

    Accepted Solution

    Ok, there is  many way you can go about this, a simple way i could think of and its open for discussions is i would use usercontrols for QuestionPossibleAnswers and add them dynamically to the site based on  NoOfPossibleAnswers Textbox....

    and the way to go about it is:

    1. you add your user controls in the page load based on the NoOfPossibleAnswers textbox and a for loop:


    if (tbx1.Text != "")
                int no = int.Parse(tbx1.Text);

                for (int i = 0; i < no; i++)

    2. Upon saving all you need to do is loop through the placeholder and save each possible answer....

    foreach (Control c in PlaceHolder1.Controls)
                if (c.GetType().ToString().Equals("ASP.questionpossibleanswers_ascx"))
                    TextBox tb1 = (TextBox)c.FindControl("tbxPossibleAnswer0").Text;
                    TextBox tb2 = (TextBox)c.FindControl("tbxPossibleAnswer1").Text;
                    //..............and so on.....

                    /*Do your saving bit here.....*/

    give me a shout if it doesn't make sense...

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now