列'PDTID'不属于表 [英] Column 'PDTID' does not belong to table

查看:124
本文介绍了列'PDTID'不属于表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,我有一个问题。我的AddToCart页面无需登录即可顺利运行。但我最近添加了登录页面。之后它显示异常列'PDTID'不属于表。怎么办?



我尝试过:



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

                }

推荐答案

您确定Access数据库确实有 PDTID 专栏?



我会改变这个:

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

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



to:


to:

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



您必须收到警告 SqlInjection 。所以,我使用 OledbCommand [ ^ ]!





我简化了您的代码:


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


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
}





详情请见:

DataColumn.Expression属性(System.Data)| Microsoft Docs [ ^ ]


这篇关于列'PDTID'不属于表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆