i have a gridview and a details view. i am trying to write, the following information, CustomerId,Qty,OrderDate,ProductID
I have made the customerid into a SESSIONID="CustomerID" and the qty,productid are in a basket session called GVBASKET-gridview
I want to insert the sessionId vairables eg customerid,qty,productid into the database or into the gridview/detailsview and then write that to the database- the database has a an ORDER table with the above values.
any help appericated.
thanks
Loading
mikePosted Jun 12, 2009, 8:11 PM
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
namespace asigment2websitefix
{
public partial class newproducts_turbo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable basket = (DataTable)Session["Basket"];
GVBasket.DataSource = basket;
GVBasket.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//When clicking through multiple pages (i.e. the multipage links at the
//bottom of GridView1, the page change triggers this Row Command event.
//This if statement stops a Basket update when its just a page change.
if (!e.CommandName.Equals("Page"))
{
//Gets the DataTable basket object out of the Session
DataTable basket = (DataTable)Session["Basket"];
//Gets the index of the row that's been clicked
int idx = int.Parse(e.CommandArgument.ToString());
//Gets the Name, Votes and Image variables from the GridView object
// string image = GridView1.Rows[idx].Cells[2].Text;
string name = GridView1.Rows[idx].Cells[0].Text;
//string SID = GridView1.Rows[idx].Cells[5].Text;
int ID = int.Parse(GridView1.Rows[idx].Cells[4].Text);
Decimal Price = Decimal.Parse(GridView1.Rows[idx].Cells[2].Text);
int Qty = 1;
//Creates a DataRow that is in the same format as the DataRows in the
//Basket DataTable (see the Page Load event above).
DataRow basketRow = basket.NewRow();
//Puts the selected row information into the just created row
// basketRow["Image"] = image;
basketRow["Name"] = name;
basketRow["ProductID"] = ID;
basketRow["Price"] = Price;
basketRow["Qty"] = Qty;
//if(basket.Rows[i]["Name"].Equals(name))
bool found = false;
for (int i = 0; i < basket.Rows.Count; i++)
{
if (basket.Rows[i]["Name"].Equals(name))
{
Qty = int.Parse( basket.Rows[i]["Qty"].ToString());
Qty = Qty + 1;
basket.Rows[i]["Qty"] = Qty;
found = true;
}
}
if (found == false)
{
basket.Rows.Add(basketRow);
}
// for(int i = 1;i<=10;i++)
// basketRow["Votes"] = votes;
// basketRow["Count"] = 1;
//Add the row to the basket
//Note: Using a loop at this stage would be a good way to check
//if the item already exists. If it does, you don't have to add
//the row, simply increment the count variable. This is for you
//to figure out. Hint:
//Connect the basket to the GVCart (the Grid View object)
GVBasket.DataSource = basket;
GVBasket.DataBind();
//Save the updated basket back into the Session
Session["Basket"] = basket;
}
}
protected void btn_Search_Turbo_Click(object sender, EventArgs e)
{
}
// make a variable called ordertoal of the decimal class and keep it private
private Decimal TotalOrder;
protected void GVBasket_DataBinding(object sender, EventArgs e)
{
TotalOrder = 0.0M;
}
protected void GVBasket_DataBound(object sender, EventArgs e)
{
lbl_total.Text = TotalOrder.ToString("C");
}
protected void GVBasket_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView DRV = e.Row.DataItem as DataRowView;
// price is in column 1
// quantity is in column 2
Decimal Price = (Decimal)DRV.Row.ItemArray[2];
int Qty = (int)DRV.Row.ItemArray[3];
Decimal RowTotal = Price * Qty;
TotalOrder += RowTotal;
}
}
protected void btn_search_Click(object sender, EventArgs e)
{
AccessDataSource1.SelectCommand = "SELECT * FROM [Products] WHERE ([Name] LIKE 'Greedy' + '%' + ? + '%')";
}
protected void Button1_Click(object sender, EventArgs e)
{
//DataRow basketRow = basket.NewRow();
// if (GVBasket.SelectedIndex > -1 && GVBasket.Rows.Count > 0)
// {
// GVBasket.RemoveAt(GVBasket.SelectedIndex);
// }
// GVBasket.DataSource = basket;
// GVBasket.DataBind();
}
protected void GVBasket_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = int.Parse(e.CommandArgument.ToString());
DataTable basket = (DataTable)Session["Basket"];
try
{
if (int.Parse(basket.Rows[index]["Qty"].ToString()) > 1)
{
basket.Rows[index]["Qty"] = int.Parse(basket.Rows[index]["Qty"].ToString()) - 1;
}
else
{
basket.Rows[index].Delete();
}
}
catch(Exception)
{
}
}
}
}
this is the products page,
and when they login in this is the source code for that page
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.OleDb;
namespace asigment2websitefix
{
public partial class checkout : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["CustomerEmail"] != null)
{
string name = (string)Session["CustomerEmail"];
lbl_username.Text = "Welcome " + name;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
string username = txtbox_username.Text;
string password = txtbox_password.Text;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].Cells[3].Text.Equals(username) &&
GridView1.Rows[i].Cells[5].Text.Equals(password))
{
Session["CustomerID"] = GridView1.Rows[i].Cells[0].Text;
Session["CustomerName"] = GridView1.Rows[i].Cells[1].Text;
Session["CustomerAddress"] = GridView1.Rows[i].Cells[2].Text;
Session["CustomerEmail"] = GridView1.Rows[i].Cells[3].Text;
Session["CustomerCreditCardNo"] = GridView1.Rows[i].Cells[4].Text;
Session["CustomerPassword"] = GridView1.Rows[i].Cells[5].Text;
}
}
Response.Redirect("Login.aspx");
}
}
}
mikePosted Jun 12, 2009, 8:08 PM
Yes I understand I need to do this,
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
namespace asigment2websitefix
{
public partial class checkoutpage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["CustomerFirstName"] == null)
{
Response.Redirect("newloginpageaspx.aspx");
string name = (string)Session["CustomerFirstName"];
//lbl_welcome_name.Text = "Welcome " + name;
}
DataTable basket = (DataTable)Session["Basket"];
GVBasket.DataSource = basket;
GVBasket.DataBind();
lbl_shipping_name.Text = Session["CustomerFirstName"].ToString();
lbl_shipping_address.Text = Session["CustomerAddress"].ToString();
lbl_shipping_suburb.Text = Session["CustomerSubarb"].ToString();
lbl_shipping_city.Text = Session["CustomerTown"].ToString();
}
protected void GVBasket_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = int.Parse(e.CommandArgument.ToString());
DataTable basket = (DataTable)Session["Basket"];
try
{
if (int.Parse(basket.Rows[index]["Qty"].ToString()) > 1)
{
basket.Rows[index]["Qty"] = int.Parse(basket.Rows[index]["Qty"].ToString()) - 1;
}
else
{
basket.Rows[index].Delete();
}
}
catch (Exception)
{
}
}
private Decimal TotalOrder;
protected void GVBasket_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView DRV = e.Row.DataItem as DataRowView;
// price is in column 1
// quantity is in column 2
Decimal Price = (Decimal)DRV.Row.ItemArray[2];
int Qty = (int)DRV.Row.ItemArray[3];
Decimal RowTotal = Price * Qty;
TotalOrder += RowTotal;
}
}
protected void GVBasket_DataBinding(object sender, EventArgs e)
{
TotalOrder = 0.0M;
}
protected void GVBasket_DataBound(object sender, EventArgs e)
{
lbl_total.Text = TotalOrder.ToString("C");
}
protected void btn_continue_checkout_Click(object sender, EventArgs e)
{
Response.Redirect("payment.aspx");
}
protected void btn_change_details_Click(object sender, EventArgs e)
{
Response.Redirect("editcustomerdetails.aspx");
}
}
}
this is my checkout page,
and the other data is stored on the masterpage
DataTable newBasket = new DataTable();
//newBasket.Columns.Add("Image", Type.GetType("System.String"));
newBasket.Columns.Add("Name", Type.GetType("System.String"));
newBasket.Columns.Add("ProductID", Type.GetType("System.Int32"));
newBasket.Columns.Add("Price", Type.GetType("System.Decimal"));
newBasket.Columns.Add("Qty", Type.GetType("System.Int32"));
Session["Basket"] = newBasket;
thanks
Puran KaushalPosted Jun 12, 2009, 8:31 AM
Store all the information in dataTable and then read each row of datatable and send it to database.
If you need more help then send your code
Regards