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

Total Post:32

Points:226
Posted on    November-26-2014 10:06 PM

 ASP.Net C#  Dropdown  ListBox 
Ratings:


 1 Reply(s)
 673  View(s)
Rate this:

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



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    November-27-2014 12:29 AM

I have found a solution, I have followed the hint given by Rick. However this causes another issue: that when you add a column below the if statement, the second time the code runs you will receive an error message because the column already exists. I have found a way around, that you can find below.

 

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

}

}

}


Don't want to miss updates? Please click the below button!

Follow MindStick