列'productid'不属于表格表。 [英] Column 'productid' does not belong to table table.

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

问题描述

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.SqlClient;
using System.Configuration;
public partial class cart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add("sno");
            dt.Columns.Add("productid");
            dt.Columns.Add("productname");
            dt.Columns.Add("quantity");
            dt.Columns.Add("price");
            dt.Columns.Add("totalprice");
            dt.Columns.Add("productimage");
            /*dt.Columns.Add(new DataColumn("sr"));
            dt.Columns.Add(new DataColumn("totalprice"));
            dt.Columns.Add(new DataColumn("productid"));
            dt.Columns.Add(new DataColumn("productname"));
            dt.Columns.Add(new DataColumn("quantity"));
            dt.Columns.Add(new DataColumn("price"));
            dt.Columns.Add(new DataColumn("productimage"));*/

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

                    dr = dt.NewRow();

                    SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                    String myquery = "select * from comprod where prodid=" + Request.QueryString["prodid"];
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = myquery;
                    cmd.Connection = scon;
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    DataSet ds = new DataSet();
                    da.Fill(ds);

                    /*for (int i = 1; i < 10; i++)
                    {
                        //DataRow dr = dt.NewRow();
                        dr["productid"] = i.ToString();
                        dr["productname"] = "productname " + i.ToString();
                        dr["price"] = i.ToString();
                        dr["quantity"] = i.ToString();
                        dr["totalprice"] = i.ToString();
                        dr["productimage"] = i.ToString();
                        dt.Rows.Add(dr);
                    }*/
                    dr["sno"] = 1;
                    dr["productid"] = ds.Tables[0].Rows[0]["productid"].ToString();
                    dr["productname"] = ds.Tables[0].Rows[0]["productname"].ToString();
                    dr["productimage"] = ds.Tables[0].Rows[0]["productimage"].ToString();
                    dr["quantity"] = Request.QueryString["quantity"];
                    dr["price"] = ds.Tables[0].Rows[0]["price"].ToString();
                    int price = Convert.ToInt16(ds.Tables[0].Rows[0]["price"].ToString());
                    int quantity = Convert.ToInt16(Request.QueryString["quantity"].ToString());
                    int totalprice = price * quantity;
                    dr["totalprice"] = totalprice;

                    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("cart.aspx");

                }
                else
                {

                    dt = (DataTable)Session["buyitems"];
                    int sr;
                    sr = dt.Rows.Count;

                    dr = dt.NewRow();

                    SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                    String myquery = "select * from comprod where prodid=" + Request.QueryString["id"];
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = myquery;
                    cmd.Connection = scon;
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    dr["sno"] = sr + 1;
                    dr["productid"] = ds.Tables[0].Rows[0]["productid"].ToString();
                    dr["productname"] = ds.Tables[0].Rows[0]["productname"].ToString();
                    dr["productimage"] = ds.Tables[0].Rows[0]["productimage"].ToString();
                    dr["quantity"] = Request.QueryString["quantity"];
                    dr["price"] = ds.Tables[0].Rows[0]["price"].ToString();
                    int price = Convert.ToInt16(ds.Tables[0].Rows[0]["price"].ToString());
                    int quantity = Convert.ToInt16(Request.QueryString["quantity"].ToString());
                    int totalprice = price * quantity;
                    dr["totalprice"] = totalprice;
                    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("cart.aspx");

                }
            }
            else
            {
                dt = (DataTable)Session["buyitems"];
                GridView1.DataSource = dt;
                GridView1.DataBind();
                if (GridView1.Rows.Count > 0)
                {
                    GridView1.FooterRow.Cells[5].Text = "Total Amount";
                    GridView1.FooterRow.Cells[6].Text = grandtotal().ToString();

                }


            }
            Label2.Text = GridView1.Rows.Count.ToString();
        }
            
        }
    
            public int grandtotal()
            {
                 DataTable dt = new DataTable();
                 dt = (DataTable)Session["buyitems"];
                 int nrow = dt.Rows.Count;
                 int i = 0;
                 int gtotal = 0;
                 while(i<nrow)
                 {
                        gtotal =gtotal+ Convert.ToInt32(dt.Rows[i]["totalprice"].ToString());

                        i = i + 1;
                  }
                return gtotal;
            }
}

/*protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    DataTable dt = new DataTable();
    dt = (DataTable)Session["buyitems"];


    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
        int sr;
        int sr1;
        string qdata;
        string dtdata;
        sr = Convert.ToInt32(dt.Rows[i]["sno"].ToString());
        TableCell cell = GridView1.Rows[e.RowIndex].Cells[0];
        qdata = cell.Text;
        dtdata = sr.ToString();
        sr1 = Convert.ToInt32(qdata);

        if (sr == sr1)
        {
            dt.Rows[i].Delete();
            dt.AcceptChanges();
            //Label1.Text = "Item Has Been Deleted From Shopping Cart";
            break;

        }
    }

    for (int i = 1; i <= dt.Rows.Count; i++)
    {
        dt.Rows[i - 1]["sno"] = i;
        dt.AcceptChanges();
    }

    Session["buyitems"] = dt;
    Response.Redirect("cart.aspx");
}
}
*/







和购物车。 aspx文件




And cart.aspx file

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True" >
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="sno" HeaderText="Sr No." />
                <asp:BoundField DataField="productid" HeaderText="Product Id" />
                <asp:ImageField DataImageUrlField="productimage" HeaderText="Image">
                </asp:ImageField>
                <asp:BoundField DataField="productname" HeaderText="Product Name" />
                <asp:BoundField DataField="price" HeaderText="Price" />
                <asp:BoundField DataField="quantity" HeaderText="Quantity" />
                <asp:BoundField DataField="totalprice" HeaderText="Total Price" />
                <asp:CommandField DeleteText="Remove" ShowDeleteButton="True" />
            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:GridView>





我尝试过:



gridview列值未标识



prodid是我的sqltable和productid是我的gridview列名。

这是我的表:



What I have tried:

gridview column values is not identifing

prodid is my sqltable and productid is my gridview column name.
this is my table:

CREATE TABLE [dbo].[comprod] (
    [prodid]      INT          IDENTITY (1, 1) NOT NULL,
    [prodname]    NCHAR (200)  NULL,
    [prodprice]   NCHAR (100)  NULL,
    [proddesc]    NCHAR (1000) NULL,
    [prodimg]     NCHAR (1000) NULL,
    [companyname] NCHAR (100)  NULL,
    [prodtype]    NCHAR (50)   NULL,
    CONSTRAINT [PK_comprod] PRIMARY KEY CLUSTERED ([prodid] ASC)
);



i引用此视频:

#2使用数据库在Asp.Net中实施完整的购物车印地语|电子商务网站|免费 - YouTube [ ^ ]

推荐答案

错误信息非常合理。



看看你的代码:

A error message is quite reasonable.

Take a look at your code:
dt.Columns.Add("productid");



和你的sql命令:


and your sql command:

String myquery = "select * from comprod where prodid=" + Request.QueryString["prodid"];



然后:


then:

dr["productid"] = ds.Tables[0].Rows[0]["productid"].ToString(); //!!!





您是否看到了列名称的不同?



注意,还有一些其他错误,例如:

1)您的代码是 Sql Injection [ ^ ]易受攻击,

2)从不使用连接文本作为查询,

3)使用参数化查询。



如需了解更多详情,请阅读我的过去答案:列'PDTID'不属于表 [ ^ ]



发出参数化查询| Microsoft Docs [ ^ ]

如何:执行参数化查询| Microsoft Docs [ ^ ]



Do you see the difference in names of column?

Note, that there's a few other mistakes, like:
1) your code is Sql Injection[^] vulnerable,
2) never use concatenated text as a query,
3) use parameterized queries.

For further details, please, read my past answer: Column 'PDTID' does not belong to table[^]
and
Issuing a Parameterized Query | Microsoft Docs[^]
How to: Execute a Parameterized Query | Microsoft Docs[^]


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

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