Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys iam developing online examination website using asp.net and c# but in the result.aspx page i want to compare the selected option and answer in the database so this is my code and its not working and not displaying any thing

What I have tried:

// submitt button click event

protected void Exambutton_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("Select Answer from [Biology]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable ddt = new DataTable();
da.Fill(ddt);
if (QuestionGrid.FindControl("Option1") == ddt.Rows[0]["Answer"])
{
result = 1;
resultlabel.Text = result.ToString();


}
Posted
Updated 5-Dec-18 13:32pm
Comments
F-ES Sitecore 5-Dec-18 5:21am    
FindControl returns an object that encapsulates the control, if your case a radio button probably. Rows[0]["Answer"] is probably text however. You can't compare an object to text, what you want to do is compare the selected value that Option1 represents with the answer.

Google how to use FindControl to cast to a specific control.

First off, make it a habit to put objects that eat resources such as SqlConnection, SqlCommand and SqlDataAdapter within a using statement to ensure that objects will be properly disposed and closed after they are used.

Second, if your QuestionGrid is a GridView, then you can't just use FindControl() method directly to it since a GridView is composed of Rows and Columns. This means that you need to traverse to its rows first before using FindControl.

Third, you can't compare the result of FindControl() method to a string. So this line if (QuestionGrid.FindControl("Option1") == ddt.Rows[0]["Answer"]) of code will fail.

Here's what you can try:
C#
protected void Exambutton_Click(object sender, EventArgs e)
{
	DataTable dt = new DataTable();
	string sqlStatement = "SELECT Answer FROM [Biology]";
   
        	using(SqlConnection connection = new SqlConnection("Your Connection String from your Web.config")){
           		using(SqlCommand cmd = new SqlCommand(sqlStatement ,connection)){
               	 	cmd.CommandType = CommandType.Text;
					using(SqlDataAdapter da = new SqlDataAdapter(cmd)){
						da.Fill(dt);
						if(dt.Rows.Count > 0){
							int index = 0;
							foreach (GridViewRow row in QuestionGrid.Rows){
								RadioButton rb = (RadioButon) row.FindControl("Option1");
								if(rb != null){
									if(rb.Text == dt.Rows[index]["Answer"]){
										//do something if matched
										rb.Checked = true;
									}
								}
								index++;
							}
						}
						else{
							//No Records found	
						}
					}
        		}
        }
}
 
Share this answer
 
Comments
AhmedHosny96 6-Dec-18 8:27am    
thank you so much brother
1st its a data-list not grid-view but i named it as a Question-grid
2nd i have four radio buttons can u please tell me how to do it for the other three radio buttons
Vincent Maverick Durano 6-Dec-18 21:02pm    
(1) If it's a DataList then you still have to loop through the DataList Items.
(2) Just use FindControl() method to access your other RadioButtons
AhmedHosny96 7-Dec-18 11:08am    
Thank you soo much brother big hugs.... this tip helped me
AhmedHosny96 7-Dec-18 8:38am    
sorry to ask you this but i want to change this datalist into gridview so can you please help?
Vincent Maverick Durano 9-Dec-18 22:08pm    
foreach (DataListItem item in QuestionGrid.Items)
{
    if (itm.ItemType == ListItemType.Item || itm.ItemType == ListItemType.AlternatingItem)
    {
        RadioButton rb = (RadioButon) item.FindControl("Option1");
        //rest of the code here
    }
}
are we assuming here your table 'biology' only contains one row?
 
Share this answer
 
Comments
AhmedHosny96 5-Dec-18 10:26am    
yes brother my table here is biology i need to compare all the 4 radiobuttons with correct answer in the database and then display the result ?
Vincent Maverick Durano 5-Dec-18 19:07pm    
Please use the "Have a Question or Comment" button when asking for clarification instead of posting it as a solution.

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