我得到DataBinding:'System.Data.DataRowView'不包含名为'productID'的属性

时间:2017-10-10 07:36:43

标签: 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'不包含属性   名为'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)
    {

    }
}

1 个答案:

答案 0 :(得分:2)

我假设您对DataAdapter使用了错误的命令或使用了错误的列。因为您希望在视图中显示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();
   }
}

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