forum

Home / DeveloperSection / Forums / Drop Down List in ASP.NET for C#. Column 'Colour' does not belong to table

Drop Down List in ASP.NET for C#. Column 'Colour' does not belong to table

Elena Glibart271026-Nov-2014

<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>

Please find below the code for the code behind file:

 

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");

}

 

}

Like I said I am having the following error (related to the 'Colour' column that is not recognised for some reason):

 

Exception Details: System.ArgumentException: Column 'Colour' does not belong to table .

 

Source Error:

 

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

 

Thank you in advance.


Updated on 27-Nov-2014

Can you answer this question?


Answer

1 Answers

Liked By