Insert into one to many relationship

Posted on 2007-07-30
Medium Priority
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 VB.net & SQL server 2005
Thanks in advance
Question by:mmalik15
  • 2

Expert Comment

ID: 19591221
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

ID: 19591308
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

Accepted Solution

here4u247 earned 1500 total points
ID: 19591525
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

807 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