DataBinding:System.Data.DataRowView不包含名为Quantity的属性

时间:2016-03-25 02:01:17

标签: c# asp.net

后端代码

请帮助确定问题

gridview中的数量字段中存在数据绑定错误,其中包含导致错误的下拉列表

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

public partial class Webpages_Cart : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectConn"].ConnectionString);

    DataTable dtCart = new DataTable();

    protected void Page_Load(object sender, EventArgs e)    
    {
    string id;       
    if (Session["bool"] != null)
    {
    bool ab = (bool)Session["bool"];           
    id = (string)Request.QueryString["Id"];
    if (ab == true)
    {
                    con.Open();
                    SqlCommand cmd = con.CreateCommand();
                    cmd.CommandText = "Select Name,Money from Image where Id=" + id;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    if (Session["Cart"] == null)
                    {
                        da.Fill(dtCart);
                        Session["Cart"] = dtCart;
                        con.Close();
                        FillCart();
                        CreateCart();
                    }
                    else
                    {
                        DataTable data = Session["Cart"] as DataTable;
                        da.Fill(dtCart);
                        dtCart.Merge(data);
                        dtCart.AcceptChanges();
                        Session["Cart"] = dtCart;
                        con.Close();
                        FillCart();
                    }

                }
                else
                {
                    if (Session["Cart"] != null)
                    {
                        dtCart = Session["Cart"] as DataTable;
                        FillCart();
                    }
                }
                if(Session["Cart"]==null)
                {
                    FillCart();
                    CreateCart();
                }
                Session["bool"] = false;
            }
        }
        protected void ddlQuantity_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                DataTable dtCart = (DataTable)Session["Cart"];
                DropDownList ddl = (DropDownList)sender;
                GridViewRow row = (GridViewRow)ddl.NamingContainer;
                if (row != null)
                {
                    int quantity = Convert.ToInt32(((DropDownList)(row.FindControl("ddlQuantity"))).SelectedValue);
                    int index = Convert.ToInt32(((DropDownList)(row.FindControl("ddlQuantity"))).SelectedIndex);
                    int price = Convert.ToInt32(dtCart.Rows[row.RowIndex]["Price"]);
                    dtCart.Rows[row.RowIndex].SetField("Quantity", (quantity));
                    dtCart.Rows[row.RowIndex].SetField("Subtotal", (quantity * price));
                    GridView1.DataSource = dtCart;
                    GridView1.DataBind();
                    Session["Cart"] = dtCart;
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("ErrorPa.aspx?Id="+ex.Message.ToString());
            }
        }
        private void CreateCart()
        {
            dtCart = new DataTable();
            dtCart.Columns.Add("Sr No", typeof(int));
            dtCart.Columns.Add("Name", typeof(string));
            dtCart.Columns.Add("Price", typeof(int));
            //dtCart.Columns.Add("Image");
            dtCart.Columns.Add("Quantity", typeof(int));
            dtCart.Columns.Add("Subtotal", typeof(int));
        }
        private void FillCart()
        {
            try
            {

                if (Session["Cart"] != null)
                {
                    dtCart = (DataTable)Session["Cart"];
                }
                DataTable dtSelProducts = (DataTable)Session["dtSelectProduct"];
                if (Session["dtSelectProduct"] != null)
                {
                    foreach (DataRow dr in dtSelProducts.Rows)
                    {
                        dtCart.Rows.Add(Convert.ToInt32(dtCart.Rows.Count + 1), Convert.ToString(dr["Name"]), Convert.ToInt32(dr["Price"]), 1, Convert.ToInt32(dr["Price"]));
                    }
                    Session["dtSelectProduct"] = null;
                }
                Session["Cart"] = dtCart;
                GridView1.DataSource = dtCart;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                Response.Redirect("ErrorPa.aspx?Id="+ex.Message.ToString());
            }
        }
        protected void imgDeleteProduct_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                DataTable dtCart = (DataTable)Session["Cart"];
                int SrNo = Convert.ToInt32(((ImageButton)sender).CommandArgument);
                dtCart.Rows.RemoveAt((SrNo - 1));

                dtCart = RegenerateDataTable(dtCart);
                GridView1.DataSource = dtCart;
                GridView1.DataBind();
                Session["Cart"] = dtCart;
                if (dtCart.Rows.Count == 0)
                {
                    btnProceed.Enabled = false;//dvSendOrder.Visible = false;
                }

                Label lblMaster = (Label)this.Master.FindControl("lblCartValue");


                //if (lblMaster != null)
                //{
                //    if (Session["Cart"] == null)
                //    {
                //        lblMaster.Text = "(0)";
                //    }
                //    else
                //    {
                //        DataTable dt = (DataTable)Session["Cart"];
                //        lblMaster.Text = "(" + Convert.ToString(dt.Rows.Count) + ")";
                //    }
                //}
            }
            catch (Exception ex)
            {
                Response.Redirect("ErrorPa.aspx?Id="+ex.Message.ToString());
            }
        }
        private DataTable RegenerateDataTable(DataTable dt)
        {
            int srno = 0;
            DataTable dtDup = dt.Clone();
            try
            {
                foreach (DataRow row in dt.Rows)
                {
                    DataRow drNew = dtDup.NewRow();
                    srno += 1;
                    drNew[0] = srno;
                    drNew[1] = row[1];
                    drNew[2] = row[2];
                    drNew[3] = row[3];
                    drNew[4] = row[4];
                    drNew[5] = row[5];
                    drNew[6] = row[6];
                    dtDup.Rows.Add(drNew);
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("ErrorPa.aspx?Id="+ex.Message.ToString());
            }
            return dtDup;
        }
    }

前端

GridView中的数量字段导致错误,因为数据未绑定

<%@ Page Title="Cart" Language="C#" ErrorPage="~/Webpages/ErrorPa.aspx" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Cart.aspx.cs" Inherits="Webpages_Cart" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <br /><br />
    <h1 class="Contact">Cart</h1>
   <%-- <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:ImageField HeaderText="Image" DataImageUrlField="Image" ReadOnly="True">
            </asp:ImageField>--%>
    <asp:GridView ID="GridView1" HorizontalAlign="Center" ShowHeaderWhenEmpty="true" runat="server" RowStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" EnableModelValidation="True"
        AutoGenerateColumns="False" CssClass="mGrid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr" EmptyDataText="There are no items in cart.">
        <AlternatingRowStyle CssClass="alt" />
        <Columns>
            <asp:BoundField HeaderText="Sr No"/>
            <%--<asp:BoundField HeaderText="ID" DataField="ProductID" />--%>
            <asp:BoundField HeaderText="Name" DataField="Name" />
            <asp:BoundField HeaderText="Price" DataField="Money" />
            <%--<asp:ImageField HeaderText="Image" DataImageUrlField="Image" />--%>
            <asp:TemplateField HeaderText="Quantity">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlQuantity" runat="server" OnSelectedIndexChanged="ddlQuantity_SelectedIndexChanged" AutoPostBack="True" SelectedValue='<%# Bind("Quantity")%>'>
                        <asp:ListItem Value="1" Text="1"></asp:ListItem>
                        <asp:ListItem Value="2" Text="2"></asp:ListItem>
                        <asp:ListItem Value="3" Text="3"></asp:ListItem>
                        <asp:ListItem Value="4" Text="4"></asp:ListItem>
                        <asp:ListItem Value="5" Text="5"></asp:ListItem>
                        <asp:ListItem Value="6" Text="6"></asp:ListItem>
                        <asp:ListItem Value="7" Text="7"></asp:ListItem>
                        <asp:ListItem Value="8" Text="8"></asp:ListItem>
                        <asp:ListItem Value="9" Text="9"></asp:ListItem>
                        <asp:ListItem Value="10" Text="10"></asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Subtotal" DataField="Subtotal" />
            <asp:TemplateField HeaderText="Delete">
                <ItemTemplate>
                    <asp:ImageButton ID="imgDeleteProduct" CommandArgument='<%# Eval("SrNo") %>' ToolTip="Remove"
                        OnClientClick="return confirm('Are you sure you want to remove this product?');" CausesValidation="False"
                        OnClick="imgDeleteProduct_Click" ImageUrl="images/delete_icon.png"
                        runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        <PagerStyle CssClass="pgr" />
        <RowStyle HorizontalAlign="Center" />
    </asp:GridView>
    <asp:Button ID="btnProceed" runat="server" Text="Button" />
</asp:Content>

0 个答案:

没有答案