-3

I have a form on a page.

The form contains of 3 known fields: Sex Age StoreId

and an unknown number of fields that all begin with "Question_" followed by an ID

I would like to store these fields in my table(MyTable) in db(MyDb). Each record I need stored contains: ID(Int), Sex(String), Age(Int), StoreID(Int), QuestionID(Int), Answer(Int)

Can someone help me get started?

I got:

public ActionResult Index(Questions model, FormCollection form) {

        var Age = form["age"];
        var StoreId = form["storeId"];
        var Sex = form["sex"];

and then I guess i will be using an array getting the last "Questions_" ready for the insert?

2
  • First try normalizing your db. Commented Jul 29, 2016 at 10:16
  • Wrong. your table should have only the first 4 columns and then put your question and answers in separate tables joined by the questionID and the ID Commented Jul 29, 2016 at 10:18

2 Answers 2

0

I can be done.
1st make table one for known fields and other to store the unknown fields(question and Answer) along with the first table primary id(foreign key exactly).
2nd. store the record accordingly and through relation crud operation can be performed.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you all. 2 tables... got it!
But how do I programmatically go around this: public ActionResult Index(Questions model, FormCollection form) { var Age = form["age"]; var StoreId = form["storeId"]; var Sex = form["sex"]; and then I guess i will be using an array getting the last "Questions_" ready for the insert?
0

Sure. Create two tables. One for storing the known columns and the other for the variable questions.

The first would look like this (say Person)

personPrimaryKey(Int), Sex(String), Age(Int), StoreID(Int)

And the second as follows (say QA)

qaPrimaryKey(Int), personPrimaryKey(Int), QuestionID(Int), Answer(Int)

Then, when you need to insert, you insert Sex, Age and StoreID in to the Person table and this will give you a primary key back for the personPrimaryKey.

Then insert all the questions and answers against the personPrimaryKey in to the QA table. This allows you to have as many or few questions and answers per person.

The construct to query this back is called a SQL Join which you need to read up on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.