Столбец "productid" не принадлежит таблице table.
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"); } } */
И файл cart.aspx
<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.
это мой стол.:
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) );
я имею в виду это видео:
#2 внедрите полную корзину покупок в Asp.Net с базой данных | хинди | сайт электронной коммерции | бесплатно - YouTube[^]
Maciej Los
И...
Вы хотя бы прочитали мой ответ?