【问题标题】:Drop Down List in ASP.NET for C#. Column 'Colour' does not belong to table用于 C# 的 ASP.NET 中的下拉列表。列“颜色”不属于表
【发布时间】:2014-03-10 12:24:42
【问题描述】:

请在附件中找到 .aspx 文件的完整代码:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="LeftContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:SqlDataSource ID="DSProduct" runat="server"> </asp:SqlDataSource>
<asp:SqlDataSource ID="DSSize" runat="server" > </asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="DSProduct" 
onitemcommand="Repeater1_ItemCommand" > 
<ItemTemplate>
<br />
<asp:Image ID="Image1" runat="server" 
ImageUrl='<%#Eval("image2","~/Image/{0}") %>' 
Height="230" Width="230"
CssClass="largeimage" />      
<h3 style="color: #FFFFFF; font-weight: bold; ">
<asp:Label ID="LName" runat="server" Text='<%# Eval("product_name")%>'>
</asp:Label> &nbsp; &nbsp; 
<asp:Label ID="LPrice" style="text-decoration: line-through;" runat="server" 
Text='<% # Convert.ToDecimal(Eval("price")).ToString("£#,##0.00") %>' Font-Size="Small" ForeColor = "Blue" >
</asp:Label>
<asp:Label ID="Label4" runat="server" Text=" £ " ForeColor="Red" BackColor="White">
</asp:Label> 
<asp:Label ID="LPrice_disc" runat="server" Text='<%# Convert.ToDecimal(Eval("discount_price")).ToString("#,##0.00") %>' 
BackColor="White" ForeColor = "Red">
</asp:Label>  
<asp:Label ID="LID" runat="server" Text='<%# Eval("product_id") %>' Visible="False">
</asp:Label> </h4>
<asp:SqlDataSource ID="DSColour" runat="server" 
ConnectionString="<%$ ConnectionStrings:XXX %>" 
ProviderName="<%$ ConnectionStrings:XXX.ProviderName %>"
SelectCommand="select colour_id, colour_name from colour">
</asp:SqlDataSource>
<asp:DropDownList ID="DDListColour" runat="server" 
DataSourceID="DSColour" DataTextField="colour_name" 
DataValueField="colour_id" 
AppendDataBoundItems="true"
OnSelectedIndexChanged="colour_SelectedIndexChanged"
AutoPostBack="True"  >
</asp:DropDownList>
<asp:SqlDataSource ID="DSSize" runat="server" 
ConnectionString="<%$ ConnectionStrings:XXX %>" 
ProviderName="<%$ ConnectionStrings:XXX.ProviderName %>"
SelectCommand="select size_id, size_name from size_t">
</asp:SqlDataSource>
<asp:DropDownList ID="DropDownListSize" runat="server" 
DataSourceID="DSSize" DataTextField="size_name" 
DataValueField="size_id" AutoPostBack="True"  >
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("product_id") %>' Visible="True">
</asp:Label>
<h5 style="color: #000000; font-weight: normal;"  > 
<asp:Label ID="Ldesc" runat="server" Text='<%# Eval("product_description")%>'  Width="500" Font-Size="Medium" Font-Bold="True">
</asp:Label></h5> 
<asp:ImageButton ID="Button1" runat="server"  CssClass="addcart" ImageUrl="~/Image/btn_buy.gif" />
</ItemTemplate>
</asp:Repeater> 
</asp:Content>

请在下面找到代码隐藏文件的代码:

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

public partial class Product : System.Web.UI.Page
{
private string DataValueField;
private string Colour;
protected void Page_Load(object sender, EventArgs e)
{
String pid = Request.QueryString["pid"];
DSProduct.ConnectionString = "Data Source=X;Persist Security Info=True;User ID=Y;Password=Z";
DSProduct.ProviderName = "System.Data.OracleClient";
DSProduct.SelectCommand = "SELECT * FROM Product WHERE product_id= '" + pid + "'";

}

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string url = HttpContext.Current.Request.Url.AbsoluteUri;
Boolean flag_found;
int i;
DataTable LocalCart = new DataTable();
LocalCart.Columns.Add("Colour",typeof(String));
DataRow dr;

Session["curURL"] = url;
if (Session["username"] == null) Response.Redirect("login.aspx");
LocalCart = (DataTable)Session["cart"];
int LocalCartItemCount = (int)Session["CartItemCount"];
Decimal LocalCartAmount = (Decimal)Session["CartAmount"];


flag_found = false;
for (i = 0; i < LocalCart.Rows.Count; i++)
{
if (LocalCart.Rows[i]["ID"].Equals(((Label)e.Item.FindControl("LID")).Text.ToString()))
{
LocalCart.Rows[i]["Quantity"] = Convert.ToInt32(LocalCart.Rows[i]["Quantity"]) + 1;
LocalCart.Rows[i]["Subtotal"] = Convert.ToInt32(LocalCart.Rows[i]["Quantity"]) * Convert.ToDecimal(LocalCart.Rows[i]["Price"]);
flag_found = true;
break;
}
}
if (!flag_found)
{
dr = LocalCart.NewRow();
dr["ID"] = ((Label)e.Item.FindControl("LID")).Text.ToString();
dr["Name"] = ((Label)e.Item.FindControl("LName")).Text.ToString();
dr["Price"] = Convert.ToDecimal(((Label)e.Item.FindControl("LPrice_disc")).Text.ToString());
dr["Quantity"] = 1;
dr["Subtotal"] = Convert.ToDecimal(dr["Price"]);
dr["Colour"] = ((DropDownList)e.Item.FindControl("DDListColour")).SelectedValue;
LocalCart.Rows.Add(dr);

}
LocalCartItemCount++;
LocalCartAmount += Convert.ToDecimal(((Label)e.Item.FindControl("LPrice_disc")).Text.ToString());
Session["Cart"] = LocalCart;
Session["CartAmount"] = LocalCartAmount;
Session["CartItemCount"] = LocalCartItemCount;
Response.Redirect("CartDisp.aspx");
}

}

就像我说的那样,我遇到了以下错误(与由于某种原因无法识别的“颜色”列有关):

异常详细信息:System.ArgumentException:列“颜色”不属于表。

来源错误:

第 62 行:dr["Quantity"] = 1; 第 63 行:dr["Subtotal"] = Convert.ToDecimal(dr["Price"]); 第 64 行:dr["Colour"] = ((DropDownList)e.Item.FindControl("DDListColour")).SelectedValue; 第 65 行:LocalCart.Rows.Add(dr);

提前谢谢你。

S

【问题讨论】:

  • 你能提供你的全部源代码吗?你在哪里得到这个错误?
  • 确保LocalCart.NewRow()这个方法正在创建一个“颜色”列。
  • 我找到了解决方案。我使用过: if (LocalCart.Columns["Colour"] != null && LocalCart.Columns["Size"] != null ) { //code } else { // 如果它们不存在,则会生成这些列 }

标签: c# asp.net drop-down-menu


【解决方案1】:

我找到了解决方案,我已按照 Rick 给出的提示进行操作。但是,这会导致另一个问题:当您在 if 语句下方添加一列时,代码第二次运行时您将收到一条错误消息,因为该列已存在。我找到了解决方法,您可以在下面找到。

if (!flag_found)
{
{   // This checks if the columns are there already
if (LocalCart.Columns["Colour"] != null && LocalCart.Columns["Size"] != null ) 
{
dr = LocalCart.NewRow();
dr["ID"] = ((Label)e.Item.FindControl("LID")).Text.ToString();
dr["Name"] = ((Label)e.Item.FindControl("LName")).Text.ToString();
dr["Price"] = Convert.ToDecimal(((Label)e.Item.FindControl("LPrice_disc")).Text.ToString());
dr["Quantity"] = 1;
dr["Subtotal"] = Convert.ToDecimal(dr["Price"]);
dr["Colour"] = ((DropDownList)e.Item.FindControl("DDListColour")).SelectedValue;
dr["Size"] = ((DropDownList)e.Item.FindControl("DDListSize")).SelectedValue;
LocalCart.Rows.Add(dr);
}
else
{   // This generates the columns if they are not there already
LocalCart.Columns.Add("Colour", typeof(String));
LocalCart.Columns.Add("Size", typeof(String));
dr = LocalCart.NewRow();
dr["ID"] = ((Label)e.Item.FindControl("LID")).Text.ToString();
dr["Name"] = ((Label)e.Item.FindControl("LName")).Text.ToString();
dr["Price"] = Convert.ToDecimal(((Label)e.Item.FindControl("LPrice_disc")).Text.ToString());
dr["Quantity"] = 1;
dr["Subtotal"] = Convert.ToDecimal(dr["Price"]);
dr["Colour"] = ((DropDownList)e.Item.FindControl("DDListColour")).SelectedValue;
dr["Size"] = ((DropDownList)e.Item.FindControl("DDListSize")).SelectedValue;
LocalCart.Rows.Add(dr);
}
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多