我如何...在ASP.NET和C#中比较选定的单选按钮和数据库中的答案 [英] How do i...compare selected radio buttons and answer in the database in ASP.NET and C#
问题描述
我尝试过:
//提交按钮点击事件
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 asSqlConnection
,SqlCommand
andSqlDataAdapter
within a using statement to ensure that objects will be properly disposed and closed after they are used.
Second, if yourQuestionGrid
is aGridView
, then you can't just useFindControl()
method directly to it since aGridView
is composed ofRows
andColumns
. This means that you need to traverse to its rows first before usingFindControl
.
Third, you can't compare the result of FindControl() method to a string. So this lineif (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屋!