Click here to Skip to main content
15,891,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, I am working on my college project to build a game Kon Banega Crorepati in C#. I have put my questions on database so that random questions can be displayed on windows form in C#. I am having trouble in fetching my questions and their options from database.
1.I want 1 question to be fetched from my database at a time and display on the form with its 4 options.
2.when someone answer the question, it display a message box.//this i can do.
3. If the answer is correct, next question should be fetched from Database and display on form.
thanks.
Posted
Updated 16-Jun-14 20:50pm
v2
Comments
Dinesh.V.Kumar 17-Jun-14 2:37am    
Kindly provide little more information on where you are facing problem..Paste code if possible so that we can help you in a better way!!

Regards,
Dinesh Kumar.V.
VisheshSaxena 17-Jun-14 2:54am    
Hello sir,
please go to the link and see this image, i want to display my question in this fashion.

Link: http://www.google.co.in/imgres?imgurl=https%3A%2F%2Flh5.ggpht.com%2Fbg5vBnkZ0AcfziH19Ffzi4H9Kgagh-54u1YS2i0rYaB-OMpsKJH07-vcFxWqtR0GIclI%253Dh900&imgrefurl=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.kbcsony&h=768&w=1280&tbnid=PZZ8MRPIysaZqM%3A&zoom=1&docid=y2x6breb9SLyuM&ei=4eOfU4_4HpK9ugSK_4KgCw&tbm=isch&ved=0CGMQMygrMCs&iact=rc&uact=3&dur=3339&page=3&start=31&ndsp=20
Thanks7872 17-Jun-14 2:41am    
Don't assume that everybody knows what is KBC. Here, you have to explain issues in a general way that everybody can understand.
VisheshSaxena 17-Jun-14 3:01am    
hello sir,
Suppose i want to ask 15 questions in my game. so i create a database, put my questions their(around 30 questions). now i want 1 question from those 30 should be displayed with its 4 options on my form in visual studio windows form at a time. how can i do that and which tool should i select from toolbox?
OriginalGriff 17-Jun-14 3:06am    
What have you tried?
Where are you stuck?

Hi First save question and 4 options in same table in different columns and give unique id for each question.Like Question,opt1,pot2,opt3,opt4,flag1,Result and add some more columns for flags based on unique id u can show question and 4 options to user,if question is display u can give some identification flag like True or False,answered or not answered.

If user answer the question u can change flag as true ,so only u can retrieve question which was not display(flag as false) question.

Regards
Aravind
 
Share this answer
 
"No i dont know how to fetch a row. please help."

OK - I've move over into the solution because this is going to need code, and it's easier to read here where I can format it correctly.

I'm not going to "give you the answer" - because there isn't "an answer", there are a huge range of possible answers, and I don't want to give you a solution that introduces things your tutor hasn't! Adding to your confusion doesn't help much... :laugh:

So, first thing to do is to find out what you do know:
1) Do you know what to do with an SqlCommand object?
2) An SqlAdapter?
3) An SqlDataReader?
3) A DataTable?

A quick look at the image you want to duplicate shows that it's probably a little advanced for your to actually look like that - it'd be quite a bit of work for me - so we'll get something working and you can make it pretty later, OK?

Some other stuff I need to know: What system are you working for? website, WinForms, WPF?
It does make a difference!

"yeah! i know about those 4 things.
1. SqlCommand object,
2.SqlAdaptor
3) An SqlDataReader
4) A DataTable.
ok, lets start from base., not concentrating on graphics, jst fetching row i.e question and its options.
i am working on winForm."


OK - that helps a lot.
The simplest way to read data from the DB then:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Question, op1, op2, op3, op4, [correct answer] FROM myTable WHERE Q_id=@ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", questionIdValue);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            if (reader.Read())
                {
                string question = (string) reader["Question"];
                ...
                }
            }
        }
    }
You can correct the column names and "fill in the blanks" for the rest of the row from that pretty easily.

(There are easier ways to read the data - a DataAdapter is less code - but then it gets more complex to use it as it is in a DataTable so this will do for the moment)

Then all you have to do is use the values:
C#
labelForQuestion.Text = question;
checkBoxForOption1.Text = option1;
...




Make sense so far?
 
Share this answer
 
v2
Comments
VisheshSaxena 18-Jun-14 4:01am    
yeah! i know about those 4 things.
1. SqlCommand object,
2.SqlAdaptor
3) An SqlDataReader
4) A DataTable.
ok, lets start from base., not concentrating on graphics, jst fetching row i.e question and its options.
i am working on winForm.
OriginalGriff 18-Jun-14 4:58am    
Answer updated
VisheshSaxena 19-Jun-14 4:38am    
Nope, sorry. nothing happened. should i use textbox to display my question? please help.

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