我如何...在ASP.NET和C#中比较选定的单选按钮和数据库中的答案 [英] How do i...compare selected radio buttons and answer in the database in ASP.NET and C#

查看:49
本文介绍了我如何...在ASP.NET和C#中比较选定的单选按钮和数据库中的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们使用asp.net和c#开发在线考试网站但是在result.aspx页面我想比较所选的选项和数据库中的答案,所以这是我的代码,它不工作,不显示任何东西



我尝试过:



//提交按钮点击事件



protected void Exambutton_Click(object sender,EventArgs e)

{

SqlCommand cmd = new SqlCommand(Select答案来自[生物学],con);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataTable ddt = new DataTable();

da.Fill(ddt);

if(QuestionGrid.FindControl(Option1)== ddt.Rows [0] [Answer])

{

结果= 1;

resultlabel.Text = result.ToString();





}

解决方案

首先,让它成为一个hab它放置吃掉资源的对象,如 SqlConnection SqlCommand SqlDataAdapter 在using语句中,以确保在使用对象后将对其进行正确处理和关闭。



其次,如果您的 QuestionGrid GridView ,那么你不能直接使用 FindControl()方法,因为 GridView 由<$ c组成$ c>行和。这意味着在使用 FindControl 之前需要先遍历其行。



三,你不能将FindControl()方法的结果与字符串进行比较。所以此行 if(QuestionGrid.FindControl(Option1)== ddt.Rows [0] [Answer])代码将失败。



以下是您可以尝试的内容:

 受保护  void  Exambutton_Click( object  sender,EventArgs e)
{
DataTable dt = new DataTable();
string sqlStatement = SELECT Answer FROM [生物学];

使用(SqlConnection connection = new SqlConnection( 您的Web.config中的连接字符串)){
using (SqlCommand cmd = new SqlCommand(sqlStatement,connection)){
cmd.CommandType = CommandType.Text;
使用(SqlDataAdapter da = new SqlDataAdapter(cmd)){
da。补(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] [ 答案]){
// 如果匹配则执行某些操作
rb。 Checked = true ;
}
}
index ++;
}
}
其他 {
// 未找到记录
}
}
}
}
}


我们在这里假设你的表'生物'只包含一行吗?


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();


}

解决方案

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:

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	
						}
					}
        		}
        }
}


are we assuming here your table 'biology' only contains one row?


这篇关于我如何...在ASP.NET和C#中比较选定的单选按钮和数据库中的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆