Click here to Skip to main content
14,389,400 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Friends, I've a problem. My AddToCart page was running Smoothly without login. But I Recently Added login page. After that it showing me exception Column 'PDTID' does not belong to table .What to do?

What I have tried:

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

public partial class AddToCart : System.Web.UI.Page
{
    static Boolean availabledesignid = false;
    static Boolean orderconfirm;
    static int quantityavailable;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)   // It was working properly, before adding this login part
        {
            Response.Redirect("~/Login/Login.aspx");
        }
        if (Session["addproduct"].ToString() == "true")
        {
            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add("sno");
            dt.Columns.Add("PDTID");
            dt.Columns.Add("PDTNAME");
            dt.Columns.Add("IMAGE");
            dt.Columns.Add("PRICE");
            dt.Columns.Add("QUANTITY");
            dt.Columns.Add("totalprice");

            if (Request.QueryString["id"] != null)
            {
                if (Session["Buyitems"] == null)
                {

                    dr = dt.NewRow();
                    String mycon = @"Provider=Microsoft.Ace.OLEDB.12.0;Data Source=C:\Users\computer\Documents\Visual Studio 2010\WebSites\WebSite3\Shop.accdb";
                    OleDbConnection scon = new OleDbConnection(mycon);
                    String myquery = "select * from Products where PDTID=" + Request.QueryString["id"];      
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.CommandText = myquery;
                    cmd.Connection = scon;
                    OleDbDataAdapter da = new OleDbDataAdapter();
                    da.SelectCommand = cmd;
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    dr["sno"] = 1;
                    dr["PDTID"] = ds.Tables[0].Rows[0]["PDTID"].ToString();   //here its showing that exception
                    dr["PDTNAME"] = ds.Tables[0].Rows[0]["PDTNAME"].ToString();
                    dr["IMAGE"] = ds.Tables[0].Rows[0]["IMAGE"].ToString();
                    dr["QUANTITY"] = Request.QueryString["QUANTITY"];
                    dr["QUANTITY"] = Request.QueryString["QUANTITY"];
                    dr["PRICE"] = ds.Tables[0].Rows[0]["PRICE"].ToString();
                    Int64 price = Convert.ToInt64(ds.Tables[0].Rows[0]["PRICE"].ToString());
                    Int64 quantity = Convert.ToInt64(Request.QueryString["QUANTITY"].ToString());
                    Int64 totalprice = price * quantity;
                    dr["totalprice"] = totalprice;
                    savecartdetail(1, ds.Tables[0].Rows[0]["PDTID"].ToString(), ds.Tables[0].Rows[0]["PDTNAME"].ToString(), ds.Tables[0].Rows[0]["IMAGE"].ToString(), "1", ds.Tables[0].Rows[0]["PRICE"].ToString(), totalprice.ToString());
                    dt.Rows.Add(dr);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();

                    Session["buyitems"] = dt;
                    GridView1.FooterRow.Cells[5].Text = "Total Amount";
                    GridView1.FooterRow.Cells[6].Text = grandtotal().ToString();
                    Response.Redirect("AddToCart.aspx");

                }
Posted
Updated 18-Mar-19 18:18pm
v4
Comments
Gerry Schmitz 18-Mar-19 16:17pm
   
At what point does it complain? The Access table might be the problem for all we know.
Maciej Los 18-Mar-19 16:45pm
   
Are you sure that Access database do have a PDTID column?
I'd change this:
SELECT * from Products where PDTID=?

to:
SELECT * from Products AS P where P.PDTID=?

You have to be warned about SqlInjection. So, i'd use OleDbCommand with paramteres!
CHill60 18-Mar-19 16:56pm
   
Looks like the solution to me
Maciej Los 18-Mar-19 17:02pm
   
I've followed your suggestion, Caroline ;)
Member 14186984 18-Mar-19 23:14pm
   
System.ArgumentException was unhandled by user code
Message=Column 'PDTID' does not belong to table .
Source=System.Data
StackTrace:
at System.Data.DataRow.GetDataColumn(String columnName)
at System.Data.DataRow.set_Item(String columnName, Object value)
at AddToCart.Page_Load(Object sender, EventArgs e) in c:\Users\computer\Documents\Visual Studio 2010\WebSites\WebSite3\AddToCart.aspx.cs:line 103
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Are you sure that Access database do have got a PDTID column?

I'd change this:
SELECT * from Products where PDTID=?

to:
SELECT * from Products AS P where P.PDTID=?

You have to be warned about SqlInjection. So, i'd use OledbCommand[^] with parameters!

[EDIT]
I'd simplify your code to this:
string sConn = @"Provider=Microsoft.Ace.OLEDB.12.0;Data Source=C:\Users\computer\Documents\Visual Studio 2010\WebSites\WebSite3\Shop.accdb;Persist Security Info =False;";
DataTable dt = new DataTable();

int pdtid = Request["id"]; //i'm not sure, but seems that conversion is needed here...
string sComm = "SELECT P.* FROM Products AS P WHERE P.PDTID=?;";

using (OleDbConnection oConn = new OleDbConnection(sConn))
    {
        oConn.Open();
        using (OleDbCommand oComm  = new OleDbCommand(sComm, oConn))
        {
            oComm.Parameters.Add(new OleDbParameter(){Value=pdtid});
            using (OleDbDataReader oRdr = oComm.ExecuteReader())
            {
                dt.Load(oRdr);
            }
        }
    }

if(dt.Rows.Count>0)
{
    DataColumn dc = new DataColumn("TotalPrice", typeof(double));
    dc.Expression = "PRICE * QUANTITY";
    dt.Columns.Add(dc);
    GridView1.DataSource = dt;
    //further instructions
}


For further details, please see:
DataColumn.Expression Property (System.Data) | Microsoft Docs[^]
   
v4
Comments
Member 14186984 18-Mar-19 23:11pm
   
Yes, I do have PDTID in Access Database
Maciej Los 19-Mar-19 3:39am
   
See updated answer. This should help you to avoid such of errors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100