Last post Apr 18, 2013 01:36 AM by Steven Cheng - MSFT
Apr 06, 2013 07:16 AM|PantelisG|LINK
i have a database named Quiz and i want to compare if the user selected answer from radiobutton list is
the same with the correct answer.
Apr 06, 2013 01:24 PM|smirnov|LINK
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString()))
SqlCommand cmd = new SqlCommand("SELECT correctAnswer FROM questions where QuestionID=@question_id", cn);
cmd.Parameters["@question_id"].Value = XXX;
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
string right_answer = rdr.ToString(); //read a value
if (right_answer == radiolist1.SelectedItem.Text)
Hope this helps.
Apr 07, 2013 10:09 AM|PantelisG|LINK
thanks for your reply smirnov but i am a bit confused. What should i put there in XXX ?
Apr 07, 2013 10:18 AM|smirnov|LINK
Apr 07, 2013 10:36 AM|PantelisG|LINK
now with number 15 (QuestionID) , when i run the quiz it shows only the QuestionID's 15
Apr 07, 2013 10:53 AM|smirnov|LINK
If your page named answer.aspx, then call it as answer.aspx?id=15 where 15 is your question id.
Change code to
cmd.Parameters["@question_id"].Value = Request.QueryString["id"];
to get requested id from the URL.
Apr 07, 2013 11:45 AM|PantelisG|LINK
Well, first of all i have 2 tables on database. One is
Apr 18, 2013 01:36 AM|Steven Cheng - MSFT|LINK
How you can compare the user selected answer with the database stored one really depend on the question/answer format and the database table structure (for storing the quesion/answer pairs). For simplicify, you can consider using ADO.NET Entity Framework
+ LINQ to query data from SQL database.
The general idea is just load the answers data from database based on the question id (or other identity) and compare them. And to avoid multiple database accessing/querying, you can consider load answers of all questions in one database query and then cache
them in your application (e.g. store in ASP.NET cache）. Then, you can use a loop in your .NET code to verify the answer of each question based on the preloaded cache data.