Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my project i want to add a question and then options to it.
i have five text boxes for entering options for that question and i want that text of each text box should be saved as new record in the table with same Question Id.
i.e if i press "ADD Options" button text of each text box should be saved as new record in table.

i know how to use control parameters but in this case there are 5 text boxes and i want to insert these as separate records with same question id as these options are for same questionHow i can do this??
Please Help me
Thnks!!!!!
Posted
Comments
CHill60 24-Feb-14 7:40am    
Have you tried anything? If there was only 1 textbox how would you save that into your database?
Charan_Kumar 24-Feb-14 7:42am    
do you need to insert five records at a time?
ravikhoda 24-Feb-14 7:46am    
if your question is already saved in database and you have your question id you can use for loop to insert question id with different textboxes. share some of your code so we can get some more idea and update you according.

Hi irfanniazi,

If I understand your problem correctly,

Ques: What are you? ID: 01

TextboxAns1: Animal TextboxAns2:Plant
TextboxAns3: Human TextboxAns4: Alien TextboxAns5: Others

Now you want to store it like

QuestID | QuesDesc | Options
01 | What are you? | Animal,Plant,Human,Alien,Others

If am right, then I would suggest you to use concatenation and separation method.

Concatenate the options with comma(any character as you wish to) into a single string and pass it to Db and save it like above mentioned.

Then while populating in UI, split it using comma operator and then put it in respective text box.

Hope this helps you a bit.


Regards,
RK
 
Share this answer
 
v2
Further to RK's solution.

RK has suggested one way to store the data in the database.

If there are always 5 options for your questions then a better way could be
QuestID, QuestDescription, Option1, Option2, Option3, Option4, Option5

Assuming you have set up QuestID as autoincrement you could then insert the data like this
C#
SQL = "INSERT into QTable (QuestDescription, Option1, Option2, Option3, Option4, Option5) "
SQL += "VALUES (@desc, @opt1, @opt2, @opt3, @opt4, @opt5)"

This is assuming that you use parameterised queries (you should, to avoid sql injection) - see this link for the rest of the detail http://www.dotnetperls.com/sqlparameter[^]

However, you have stated that you want them as separate records - this would give you the advantage that different questions could have a different number of options.

In this case you should have two tables - Questions and Options. The Question table would have
QuestID and QuestDescription

The options table would have (at least)
OptionID, QuestID, OptionDescription

OptionID - not compulsory but recommended - use an Autoincrement column.
QuestID - is the Foreign Key[^] to the Questions table
OptionDescription - is the text of the option

You would then just loop through each textbox (you haven't stated your naming convention, you could even just use 5 lines of code and name them explicitly)
C#
SQL = "INSERT INTO Options (QuestId, OptionDescription) VALUES (@questID, @option)"


Later when you are attempting to get the data back out of the database you could use
SQL
SELECT QuestID, QuestDescription, Option 
FROM Questions Q 
INNER JOIN Options O ON Q.QuestID=O.QuestID
WHERE QuestID = @questid
ORDER BY O.OptionID
or use PIVOT[^] if you want them all back in a single row

[EDIT] - OP has confirmed he has a separate table for the options (good), and I have looked up the thing that was pestering the back of my mind ... To insert them all in a single query try something like this
SQL
INSERT INTO OPTIONS (QuestId, Optiontext) VALUES 
(@questID, @textbox1),
(@questID, @textbox2),
(@questID, @textbox3),
(@questID, @textbox4),
(@questID, @textbox5)) 
 
Share this answer
 
v2
Comments
irfanniazi 24-Feb-14 8:23am    
thnks for guiding me but i have a seprate table options(optionid,optiontext,Questionid) how i can insert in this table
CHill60 24-Feb-14 8:32am    
I've updated my solution
irfanniazi 24-Feb-14 9:27am    
cmd2 = new SqlCommand("Insert into Choices(Choicetext,Questionid) Values (@ctext,@qid)");
cmd2.Parameters.AddWithValue("@ctext",TextBox6.Text);
cmd2.Parameters.AddWithValue("@qid",qid);
i am doing with above code. actually i do not know how to get text of each textbox as in above case you see that only one option will be saved i.e text of TextBox6 and what about remaining four textboxes??
irfanniazi 24-Feb-14 9:39am    
i tried this thing
cmd2.Parameters.AddWithValue("@ctext",TextBox6.Text);
cmd2.Parameters.AddWithValue("@ctext", TextBox7.Text);
cmd2.Parameters.AddWithValue("@ctext", TextBox8.Text);
cmd2.Parameters.AddWithValue("@ctext", TextBox9.Text);
but gets the following error
The variable name '@ctext' has already been declared. Variable names must be unique within a query batch or stored procedure
i had tried this before posting the Question and now again geting same error .
CHill60 24-Feb-14 12:41pm    
It's because you are using @ctext for every parameter - you need a separate one for each textbox ... e.g. @ctext1, @ctext2 etc

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900