c# - 我得到数据绑定(bind) : 'System.Data.DataRowView' does not contain a property with the name 'productID'

标签 c# mysql data-binding

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="check.aspx.cs" Inherits="check" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 <style>
            h1 {
            font-family: 'Lucida Handwriting';
            text-align: center;
            margin: 50px;
            font-size: 50px;
        }

        .word {
            font-family: 'Lucida Handwriting';
            margin: 50px;
            font-size: 20px;
        }

        body {
            background-image: url("http://3.bp.blogspot.com/-3UIjuTrbAI8/Uihk5uaH_lI/AAAAAAAAAuk/gcn5XlQ9kyI/s640/Top+10+best+Simple+Awesome+Background+Images+for+Your+Website+or+Blog2.jpg");
            background-repeat: no-repeat;
            background-size: cover;
            background-attachment: fixed;
        }

        .topnav {
            overflow: hidden;
            background-color: black;
        }
        .transbox {
            margin: 30px;
            background-color: white;
            border: 0px;
            opacity: 0.75;
            /*filter: alpha(opacity=50);*/
        }

            .topnav a {
                float: left;
                display: block;
                color: #f2f2f2;
                text-align: center;
                padding: 14px 16px;
                text-decoration: none;
                font-size: 17px;
            }

                .topnav a:hover {
                    background-color: burlywood;
                    color: black;
                }

                .topnav a.active {
                    background-color: #4CAF50;
                    color: white;
                }

        .topnar a {
            float: right;
            background-color: black;
            display: block;
            color: white;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
            font-size: 17px;
        }

            .topnar a:hover {
                background-color: burlywood;
                color: black;
            }

        .txtstyle {
            width: 300px;
            padding: 12px 20px;
            margin: 8px 0;
            box-sizing: border-box;
            border: 3px solid #ccc;
            -webkit-transition: 0.5s;
            transition: 0.5s;
            outline: none;
        }

            .txtstyle:focus {
                border: 3px solid #555;
            }

             .btnlogout {
                width: 100px;
                height: 30px;
                font-family:'Lucida Handwriting';
                float:right;
            }
     .auto-style2 {
         width: 146px;
     }
     .auto-style3 {
         width: 233px;
     }
     .auto-style5 {
         width: 172px;
     }
     .auto-style6 {
         width: 141px;
     }
     .auto-style7 {
         width: 233px;
         height: 26px;
     }
     .auto-style10 {
         width: 141px;
         height: 26px;
     }
     .auto-style11 {
         height: 26px;
         width: 140px;
     }
     .auto-style14 {
         width: 175px;
     }
     .auto-style15 {
         height: 26px;
         width: 175px;
     }
     .auto-style16 {
         height: 26px;
         width: 172px;
     }
     .auto-style17 {
         height: 26px;
         width: 146px;
     }
    </style>
</head>
<body>
    <form id="form1" runat="server">

            <div class="topnar">
                <a href="adminpage.aspx">Manage</a>
                <a href="orderlist.aspx">Orderlist</a>
            </div>
            <div class="topnav">
                <a class="active" href="menuhome.aspx">Home</a>
                <a href="menuchicken.aspx">Chicken & Fish</a>
                <a href="menupasta.aspx">Pasta</a>
                <a href="menuwrap.aspx">Sandwich & Wrap</a>
                <a href="menuside.aspx">Side Order</a>
                <a href="menusoup.aspx">Salad & Soup</a>

            </div>
        <div>
            Order ID :
            <asp:TextBox ID="txtorid" runat="server"></asp:TextBox>
            Secret Code :
            <asp:TextBox ID="txtcode" runat="server"></asp:TextBox>
            <asp:Button ID="btncheck" runat="server" Text="Check" OnClick="btncheck_Click" />
            <br />
            <br />
            </div>
            <div class="transbox">
            Reservation details:
         <table border="1">
             <tr>
                 <td class="auto-style7">ID</td>
                 <td class="auto-style15">Reserved Date</td>
                 <td class="auto-style11">Reserved Time</td>
                 <td class="auto-style10">Name</td>
                 <td class="auto-style11">Handphone</td>
                 <td class="auto-style17">TableNumber</td>
                 <td class="auto-style16">Pax</td>

             </tr>

             <asp:ListView ID="listView" runat="server">

                 <ItemTemplate>

                     <tr>
                         <td><%# Eval("ID") %></td>
                         <td><%# Eval("Datebook") %></td>
                         <td><%# Eval("TimeFrom") %></td>
                         <td><%# Eval("Name") %></td>
                         <td><%# Eval("Handphone") %></td>
                         <td><%# Eval("TableNumber") %></td>
                         <td><%# Eval("Pax") %></td>


                     </tr>
                 </ItemTemplate>
             </asp:ListView>

         </table>
            <br />
            <br />
         Food Ordered:
        <table border="1">
            <tr>
                <td>Food ID</td>
                <td>Food Name</td>
                <td>Quantity</td>
                <td>Price</td>
                <td>Total</td>

            </tr>

            <asp:ListView ID="listView1" runat="server">

                <ItemTemplate>

                    <tr>
                        <td><%# Eval("productid") %></td>
                        <td><%# Eval("Foodname") %></td>
                        <td><%# Eval("quantity") %></td>
                        <td><%# Eval("price") %></td>
                        <td><%# Eval("total") %></td>
                    </tr>
                </ItemTemplate>
            </asp:ListView>

        </table>

        </div>

    </form>
</body>
</html>

嗨,这是我的代码。我使用子查询来组合 SQL 语句中的某些字段,但无法获取子查询后的值。另外,我收到此错误:

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

有人知道怎么解决吗?我已经检查了所有列名称。该sql语句可以在MYSQL数据库中运行,但不能在代码中运行。谢谢!!

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 MySql.Data.MySqlClient;
using System.IO;
using System.Text;
using System.Data;
using System.Configuration;

public partial class check : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MySqlConnection connection = new MySqlConnection();
        MySqlCommand cmmd = new MySqlCommand();
        connection.ConnectionString = "SERVER=localhost;DATABASE=pf;UID=root;PASSWORD=password;";
        connection.Open();
        string cmdText = "SELECT * FROM booking where secret = '"+ txtcode.Text + "' and id = '"+ txtorid.Text +"'";
        MySqlDataAdapter adapt = new MySqlDataAdapter(cmdText, connection);
        DataTable t = new DataTable();
        adapt.Fill(t);
        listView.DataSource = t;
        listView.DataBind();
        listView.Visible = true;
        connection.Close();

        MySqlConnection Conn = new MySqlConnection("Server=localhost;Database=pf;Uid=root;Pwd=password;");
        MySqlCommand Command = new MySqlCommand();
        Conn.Open();
        Command.Connection = Conn;
        Command.CommandText = "SELECT productid, (select name from menu where id = productid)as Foodname, price, quantity, price * quantity as total from salesdetail where salesid = (select id from booking where secret = 'ypnok3bd' and id = 'SSD00000000000000001') ";

        string productid = Command.ExecuteScalar().ToString();
        string Foodname = Command.ExecuteScalar().ToString();
        string price = Command.ExecuteScalar().ToString();
        string total = Command.ExecuteScalar().ToString();
        DataTable d = new DataTable();
        adapt.Fill(d);
        listView1.DataSource = d;
        listView1.DataBind();
        listView1.Visible = true;
        Conn.Close();

    }

    protected void btncheck_Click(object sender, EventArgs e)
    {

    }
}

最佳答案

我假设您对 DataAdapter 使用了错误的命令或使用了错误的列。因为您希望在 View 中显示 ProductID 但另一个 SelectCommand 选择该列。您用于适配器的适配器使用 SELECT * FROM booking,该表的主列是 Id


除此之外,您不能使用 ExecuteScalar 从选择查询接收多个不同的列。仅返回第一条记录的第一列。相反,您可以使用DataReader:

Command.CommandText = "SELECT productid, (select name from menu where id = productid)as Foodname, price, quantity, price * quantity as total from salesdetail where salesid = (select id from booking where secret = 'ypnok3bd' and id = 'SSD00000000000000001') ";

using(var rd = Command.ExecuteReader())
{
    if(rd.Read())
    {
       string productid = rd.GetInt32(0).ToString();
       string foodname = rd.GetString(1);
       string price = rd.GetDecimal(2).ToString();
       string total = rd.GetDecimal(3).ToString();
   }
}

我已经推测了列的类型。如果出现异常,请进行相应更改。

关于c# - 我得到数据绑定(bind) : 'System.Data.DataRowView' does not contain a property with the name 'productID' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46661069/

相关文章:

c# - 为什么自托管服务在处理一个在 Mono 上运行的响应后会挂起?

c# - 显示带缩进的父/子名称

mysql - 使用复合主键的 RIGHT JOIN 不检索所有数据

mysql - 使用 "IN"缓慢查询 MySQL

java - Android - 动态gridview(多列绑定(bind)行)

c# - Entity Framework 6 模拟包含 dbset 上的方法

c# - 堆栈溢出 ToArray()

mysql - zend 框架自动更改查询

wpf - 如何绑定(bind)到字典中的项目?

asp.net-mvc - MVC 3 - 使用 List 类型属性绑定(bind)到复杂类型