hi
i have a datagrid that include a textbox in every rows
datagrid is a shopping cart and textbox is for get Quantity of each product
how can i do that users set Quantity in the textbox and sum that with total amount?
here is my code
but there is error
code of buyproducts page
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb;
public partial class product_details : System.Web.UI.Page { protected static string productName, productcost, productQuantity; protected DataTable objDT; protected DataRow objDR; protected string product; protected Boolean blnMatch = false; protected int quantity = 1;
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) makeCart();
string m; m = Request["id"];
string c = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("shop.mdb"); OleDbConnection con = new OleDbConnection(c); string k; k = "select * from MyProducts where code =" + m; OleDbCommand cmd = new OleDbCommand(k, con); OleDbDataReader dr; con.Open(); dr = cmd.ExecuteReader(); dr.Read(); Page.Header.Title = dr["title"].ToString(); productName = dr["title"].ToString(); productcost = dr["cost"].ToString(); con.Close(); }
protected void makeCart() { objDT = new System.Data.DataTable("Cart"); objDT.Columns.Add("ID", typeof(int)); objDT.Columns["ID"].AutoIncrement = true; objDT.Columns["ID"].AutoIncrementSeed = 1;
objDT.Columns.Add("Quantity", typeof(int)); objDT.Columns.Add("Product", typeof(string)); objDT.Columns.Add("Cost", typeof(decimal));
Session["Cart"] = objDT; }
public Decimal GetItemTotal() { DataTable objDT = (DataTable)Session["Cart"]; Decimal costprod; int intCounter, quantityprod; Decimal decRunningTotal = 0; for (intCounter = 0; intCounter <= objDT.Rows.Count - 1; intCounter++) { objDR = objDT.Rows[intCounter]; costprod = (Decimal)objDR["Cost"]; quantityprod = int.Parse(objDR["Quantity"].ToString()); decRunningTotal += costprod * quantityprod; } return decRunningTotal; } protected void AddToCart(object sender, ImageClickEventArgs e) { UserControl cart = (UserControl)Master.FindControl("cart1"); DataGrid mydg = (DataGrid)cart.FindControl("dg"); Label lbl = (Label)cart.FindControl("lblTotal"); TextBox txtQuantity = (TextBox)mydg.FindControl("TextBox1"); //product = productName; DataTable objDT = (DataTable)Session["Cart"]; foreach (DataRow objDR in objDT.Rows) { txtQuantity.Text = "1"; // TextBox txtQuantity = (TextBox)mydg.FindControl("TextBox1"); objDR["Quantity"] = txtQuantity; if (objDR["product"] == productName) { // objDR["Quantity"] = int.Parse(objDR["Quantity"].ToString()) + int.Parse(TextBox1.Text); // txtQuantity.Text = objDR["Quantity"].ToString(); // objDR["Quantity"] += TextBox1.Text; objDR["Quantity"] = int.Parse(objDR["Quantity"].ToString()) + int.Parse(txtQuantity.Text); blnMatch = true; } } if (blnMatch == false) { objDR = objDT.NewRow(); objDR["Quantity"] =1; // txtQuantity.Text = objDR["Quantity"].ToString(); objDR["Product"] = productName; objDR["Cost"] = Decimal.Parse(productcost); objDT.Rows.Add(objDR); }
Session["Cart"] = objDT; if (objDT != null) { mydg.DataSource = objDT; mydg.DataBind(); lbl.Text = "????" + GetItemTotal(); } else { Response.Write("Cart is Null"); } }
|
i has problem with Quantity
here is cart usercontrol that is my shopping cart
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class cart : System.Web.UI.UserControl { DataTable objDT; DataRow objDR; string product; Boolean blnMatch = false; protected static string Reservation_Number,Redirect_URL,Total_Amount,Merchant_ID;
protected void Page_Load(object sender, EventArgs e) { // foreach (DataRow objDR in objDT.Rows) // { // TextBox txt = (TextBox)dg.FindControl("TextBox1"); // txt.Text = "100"; //} }
public Decimal GetItemTotal() { DataTable objDT = (DataTable)Session["Cart"]; Decimal costprod, quantityprod; int intCounter; Decimal decRunningTotal = 0; for (intCounter = 0; intCounter <= objDT.Rows.Count - 1; intCounter++) { objDR = objDT.Rows[intCounter]; costprod = (Decimal)objDR["Cost"]; quantityprod = int.Parse(objDR["Quantity"].ToString()); decRunningTotal += costprod * quantityprod; } return decRunningTotal; }
protected void TextBox1_TextChanged(object sender, EventArgs e) { foreach (DataRow objDR in objDT.Rows) { TextBox txt = (TextBox)dg.FindControl("TextBox1"); objDR["Quantity"] = txt.Text; lblTotal.Text = "????" + GetItemTotal(); } } protected void Delete_Item(object source, DataGridCommandEventArgs e) { objDT = (DataTable)Session["Cart"]; // objDT.Rows[e.RowIndex].Delete(); objDT.Rows[e.Item.ItemIndex].Delete(); Session["Cart"] = objDT; dg.DataSource = objDT; dg.DataBind(); lblTotal.Text = "????" + GetItemTotal(); } }
|
my cart design is here
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="cart.ascx.cs" Inherits="cart" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <asp:DataGrid ID="dg" runat="server" OnDeleteCommand="Delete_Item" ShowHeader="False"> <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Delete" ImageUrl="~/picture/delrecord.png" /> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" Style="font-size: 9pt; color: #000000; direction: rtl; font-family: tahoma; text-align: center" Width="65px"></asp:TextBox> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> Total Amount : <asp:Label ID="lblTotal" runat="server"></asp:Label> <br /> <br />
|
i can't set the txtQuantity
in this code i set it to 1 that there isn't any problem in add first product
but if i add same product again
error : object not set to an instance ....