错误:- 数据绑定:“System.Data.DataRowView"不包含名为“Optiont4"的属性 [英] ERROR :- DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Optiont4'

查看:49
本文介绍了错误:- 数据绑定:“System.Data.DataRowView"不包含名为“Optiont4"的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.aspx 页面中,我采用了标签控件来显示数据库中的问题,4 个单选按钮以显示与特定问题相关的四个选项,最后但并非最不重要的是,我使用了隐藏字段,我将在其中存储特定问题的答案.最后,我选择了一个 Button Control,为此我创建了 onclick 事件,我将在该事件上执行生成分数的操作.

.aspx.cs 页面中,在按钮的 onclick 事件上,我使用下面提到的代码从 aspx 页面中获取控件,并进一步使用了 if 语句为了查看哪个单选按钮处于活动状态并将相应的值存储在变量selans"中,使用这个selans",我将它与隐藏字段的值进行比较,以找出选中的单选按钮是正确答案还是不是,答案是正确的,即selans"中的值与隐藏字段中的值(实际答案)相匹配,变量count"(初始值为 0)相应地递增,所有这些代码都放在for 循环"将执行到 no.GridView 中的控件数量(您可以将其与问题编号联系起来,因为 GridView 为每条记录生成新控件).

但是当我运行它时,我收到此错误:-

DataBinding: 'System.Data.DataRowView' 不包含名为 'Optiont4' 的属性.说明:在执行当前 Web 请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息.异常详细信息:System.Web.HttpException:DataBinding:System.Data.DataRowView"不包含名为Optiont4"的属性.源错误:第 115 行:<asp:RadioButton ID="rad2" runat="server" Text='<%#Eval("Option2") %>'GroupName="A"/>第 116 行:<asp:RadioButton ID="rad3" runat="server" Text='<%#Eval("Option3") %>'GroupName="A"/>第 117 行:<asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Optiont4")%>'GroupName="A"/>第 118 行:<asp:HiddenField ID="hf" runat="server" Value='<%#Eval("CorrectAns")%>'/>第 119 行:源文件:e:\Way2Success\Student\Examdemo.aspx 行:117 

这里的错误行号是 30 in .aspx 页面

看看我的代码.告诉我我是否弄错了以及解决方案是什么.

.aspx :-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Examdemo.aspx.cs" Inherits="Student_Examdemo"%><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><title></title><身体><form id="form2" runat="server"><div><div id="标签"><ul><li><a href="#tabs-1">标签 1</a></li><li><a href="#tabs-2">标签 2</a></li><li><a href="#tabs-3">标签 3</a></li><li><a href="#tabs-4">标签 4</a></li><li><a href="#tabs-5">标签 5</a></li><div id="tabs-1"><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ><列><asp:TemplateField><项目模板><asp:Label ID="Label1" runat="server" Text='<%#Eval("Question") %>'></asp:Label><br/><br/><br/><asp:RadioButton ID="rad1" runat="server" Text='<%#Eval("Option1") %>'GroupName="A"/><asp:RadioButton ID="rad2" runat="server" Text='<%#Eval("Option2") %>'GroupName="A"/><asp:RadioButton ID="rad3" runat="server" Text='<%#Eval("Option3") %>'GroupName="A"/><asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Option4") %>'GroupName="A"/><asp:HiddenField ID="hf" runat="server" Value='<%#Eval("CorrectAns") %>'/><br/><br/></ItemTemplate></asp:TemplateField></列></asp:GridView>

<div id="tabs-2"><asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" ><列><asp:TemplateField><项目模板><asp:Label ID="Label1" runat="server" Text='<%#Eval("Question") %>'></asp:Label><br/><br/><br/><asp:RadioButton ID="rad1" runat="server" Text='<%#Eval("Option1") %>'GroupName="A"/><asp:RadioButton ID="rad2" runat="server" Text='<%#Eval("Option2") %>'GroupName="A"/><asp:RadioButton ID="rad3" runat="server" Text='<%#Eval("Option3") %>'GroupName="A"/><asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Optiont4")%>'GroupName="A"/><asp:HiddenField ID="hf" runat="server" Value='<%#Eval("CorrectAns")%>'/><br/><br/></ItemTemplate></asp:TemplateField></列></asp:GridView>

<div id="tabs-3">表 3 内容

<div id="tabs-4">表 4 内容

<div id="tabs-5">表 5 内容

<input type="button" id="btnPrevious" value="Previous" style="display:none"/><input type="button" id="btnNext" value="Next"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button class="panelButton" runat="server" Text="完成考试" ClientIDMode="Static" OnClick="btn_Click"/><br/>

</表单></html>

.aspx.cs :-

使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.UI;使用 System.Web.UI.WebControls;使用 System.Data.SqlClient;使用 System.Data;使用 System.Configuration;公共部分类 Student_Examdemo : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){如果 (!IsPostBack){GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");GridView1.DataBind();GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");GridView2.DataBind();私有数据集 GetData(字符串查询){string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;SqlCommand cmd = 新的 SqlCommand(查询);使用 (SqlConnection con = new SqlConnection(conString)){使用 (SqlDataAdapter sda = new SqlDataAdapter()){cmd.Connection = con;sda.SelectCommand = cmd;使用 (DataSet ds = new DataSet()){sda.Fill(ds);返回 ds;}}}}protected void btn_Click(object sender, EventArgs e){单选按钮 r1、r2、r3、r4;隐藏字段高清;整数计数 = 0;整数负 = 0;总计;int totalf=0;整数总计=0;整体;int totalfo;int totalfi;字符串 selans = "-1";for (int i = 0; i < GridView1.Rows.Count; i++){r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");如果(r1.Checked){selans = r1.Text;}否则如果(r2.Checked){selans = r2.Text;}否则如果(r3.Checked){selans = r3.Text;}否则如果(r4.Checked){selans = r4.Text;}if (hdn.Value == selans){计数++;}别的{否定--;}totalf = 计数 + 负数;}for (int i = 0; i < GridView2.Rows.Count; i++){r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");如果(r1.Checked){selans = r1.Text;}否则如果(r2.Checked){selans = r2.Text;}否则如果(r3.Checked){selans = r3.Text;}否则如果(r4.Checked){selans = r4.Text;}if (hdn.Value == selans){计数++;}别的{否定--;}总数 = 计数 + 负数;}总计 = 总计 + 总计;会话[分数"] = 总;}}

解决方案

您的 GridView2 定义中有输入错误.

<asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Optiont4")%>'GroupName="A"/>

应该

<asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Option4")%>'GroupName="A"/>

In .aspx page , I’ve taken Label Control in order to display question form the database, 4 radio buttons in order to display four option related to a particular question, and last but not the least, I’ve used hidden field in which I will store answer of the particular question. And finally I’ve taken a Button Control, for which I’ve created the onclick event, on which I will perform the operation to generate score.

In .aspx.cs page, on the onclick event of the button, , I’ve fetch the controls form the aspx page using code mentioned below, and further I’ve used if statement in order to see which radio button is active and have store the corresponding value in the varialbe "selans", using this "selans", I will compare it with the value of hidden field in order to find whether checked radio button is the correct answer or not, it the answer is correct, i.e value in "selans" matches with the value in hidden field ( the actual answer) and the variable "count" ( initially initialized with value 0) increments accordingly, and all this code is placed in the "for loop" which will execute till the no. of controls in the GridView (you can relate it with the no. of question, as for every record GridView generates new control).

But when I run it I am getting this error :-

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Optiont4'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Optiont4'.

Source Error: 


Line 115:            <asp:RadioButton ID="rad2" runat="server" Text='<%#Eval("Option2") %>' GroupName="A" />
Line 116:            <asp:RadioButton ID="rad3" runat="server" Text='<%#Eval("Option3") %>' GroupName="A" />
Line 117:            <asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Optiont4")%>' GroupName="A" />
Line 118:            <asp:HiddenField ID="hf" runat="server" Value='<%#Eval("CorrectAns")%>' />
Line 119:

Source File: e:\Way2Success\Student\Examdemo.aspx    Line: 117 

Here the error line number is 30 in .aspx page

Have a look at my code. Show me were I am making mistaking and what is the solution.

.aspx :-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Examdemo.aspx.cs" Inherits="Student_Examdemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
  </head>
<body>
    <form id="form2" runat="server">
        <div>
<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Tab 1</a></li>
        <li><a href="#tabs-2">Tab 2</a></li>
        <li><a href="#tabs-3">Tab 3</a></li>
        <li><a href="#tabs-4">Tab 4</a></li>
        <li><a href="#tabs-5">Tab 5</a></li>
    </ul>
    <div id="tabs-1">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("Question") %>'></asp:Label>
            <br />
            <br />
            <br />
           <asp:RadioButton ID="rad1" runat="server" Text='<%#Eval("Option1") %>' GroupName="A" />
            <asp:RadioButton ID="rad2" runat="server" Text='<%#Eval("Option2") %>' GroupName="A" />
            <asp:RadioButton ID="rad3" runat="server" Text='<%#Eval("Option3") %>' GroupName="A" />
            <asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Option4") %>' GroupName="A" />
            <asp:HiddenField ID="hf" runat="server" Value='<%#Eval("CorrectAns") %>' />

        <br />
        <br />

                 </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
   
    </div>
    <div id="tabs-2">
        

        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("Question") %>'></asp:Label>
            <br />
            <br />
            <br />
           <asp:RadioButton ID="rad1" runat="server" Text='<%#Eval("Option1") %>' GroupName="A" />
            <asp:RadioButton ID="rad2" runat="server" Text='<%#Eval("Option2") %>' GroupName="A" />
            <asp:RadioButton ID="rad3" runat="server" Text='<%#Eval("Option3") %>' GroupName="A" />
            <asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Optiont4")%>' GroupName="A" />
            <asp:HiddenField ID="hf" runat="server" Value='<%#Eval("CorrectAns")%>' />

        <br />
        <br />

                 </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

    </div>
    <div id="tabs-3">
        Tab 3 Content
    </div>
     <div id="tabs-4">
        Tab 4 Content
    </div>
     <div id="tabs-5">
        Tab 5 Content
    </div>
</div>
<input type="button" id="btnPrevious" value="Previous" style = "display:none"/>
<input type="button" id="btnNext" value="Next" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           
            <asp:Button class="panelButton" runat="server" Text="Finish the exam" ClientIDMode="Static" OnClick="btn_Click" />
            <br />
    </div>
    </form>
</body>
</html>

.aspx.cs :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class Student_Examdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
    {
        GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
        GridView1.DataBind();
        GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");
        GridView2.DataBind();

private DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);
                return ds;
            }
        }
    }
}

protected void btn_Click(object sender, EventArgs e)
{

            RadioButton r1, r2, r3, r4;
            HiddenField hdn;
            int count = 0;
            int neg = 0;
            int total;
            int totalf=0;
            int totals=0;
            int totalt;
            int totalfo;
            int totalfi;
            string selans = "-1";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
                r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
                r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
                r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
                hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
                if (r1.Checked)
                {
                    selans = r1.Text;
                }
                else if (r2.Checked)
                {
                    selans = r2.Text;
                }
                else if (r3.Checked)
                {
                    selans = r3.Text;
                }
                else if (r4.Checked)
                {
                    selans = r4.Text;
                }

                if (hdn.Value == selans)
                {
                    count++;
                }
                else
                {
                   neg--;
                }

                totalf = count + neg;


            }

    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
        r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");
        r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");
        r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");
        r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");
        hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");
        if (r1.Checked)
        {
            selans = r1.Text;
        }
        else if (r2.Checked)
        {
            selans = r2.Text;
        }
        else if (r3.Checked)
        {
            selans = r3.Text;
        }
        else if (r4.Checked)
        {
            selans = r4.Text;
        }

        if (hdn.Value == selans)
        {
            count++;
        }
        else
        {
            neg--;
        }

        totals = count + neg;

    }
    total = totalf + totals;
    Session["score"] = total;

}


}

解决方案

You have a typing error in your GridView2 definition.

<asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Optiont4")%>' GroupName="A" />

should be

<asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Option4")%>' GroupName="A" />

这篇关于错误:- 数据绑定:“System.Data.DataRowView"不包含名为“Optiont4"的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆