|
|
|
asp.net ระบบตระกร้าสินค้า รบกวนช่วยดูการบันทึกค่าที่แสดงใน GridView ให้ทีครับผม |
|
|
|
|
|
|
|
ผมลองฝึกทำระบบตระกร้าสินค้าตามลิ้งนี้ครับ
https://www.thaicreate.com/free-web-script/asp-net-create-shopping-cart-step-by-step.html
ในหน้า ViewCart.aspx.cs อยากทราบว่าถ้าเราต้องการเพิ่ม "ราคารวม" ลงใน ODt_Price ต้องเขียนยังไงหรอครับ
เพราะ lblTotal ไม่ได้ bind กับ ODt_Price อะครับ
Code (ASP)
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.Text;
public partial class Viewcart : System.Web.UI.Page
{
protected double strTotal = 0;
protected double strSumTotal = 0;
protected void Page_Load(object sender, EventArgs e)
{
df();
BindGird();
this.lblSumTotal.Text = strSumTotal.ToString("#,###.00");
}
protected void connect_db()
{
try
{
dbcon.connecToDB();
Response.Write("เชื่อมต่อฐานข้อมูลสำเร็จ<br />");
}
catch (Exception ex)
{
Response.Write("เชื่อมต่อฐานข้อมุลไม่สำเร็จ <br />" + ex);
}
}
protected void df()
{
if (Session["cususer"] == null)
{
Response.Redirect("Login.aspx");
}
}
protected void BindGird()
{
DataTable dt = (DataTable)Session["myCart"];
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Del")
{
int RowsID = Convert.ToInt32(e.CommandArgument);
DataTable dt = null;
dt = (DataTable)Session["myCart"];
dt.Rows[RowsID].Delete();
Session["myCart"] = dt;
Response.Redirect("ViewCart.aspx");
}
}
protected DataTable getProductDet(string strProductID)
{
connect_db();
SqlCommand cmd = new SqlCommand("select * from [Product] WHERE Pro_ID = " + strProductID + " ", dbcon.conn);
SqlDataReader read = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(read);
dbcon.closeDB();
return dt;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//*** ProductID ***//
Label lblProductID = (Label)e.Row.FindControl("lblProductID");
if ((lblProductID != null))
{
lblProductID.Text = DataBinder.Eval(e.Row.DataItem, "Pro_ID").ToString();
}
DataTable dt = getProductDet(DataBinder.Eval(e.Row.DataItem, "Pro_ID").ToString());
//*** ProductName ***//
Label lblProductName = (Label)e.Row.FindControl("lblProductName");
if ((lblProductName != null))
{
lblProductName.Text = dt.Rows[0]["Pro_Name"].ToString();
}
//*** Price ***//
Label lblPrice = (Label)e.Row.FindControl("lblPrice");
if ((lblPrice != null))
{
lblPrice.Text = dt.Rows[0]["Pro_PricePerUnit"].ToString();
}
//*** Qty ***//
Label lblQty = (Label)e.Row.FindControl("lblQty");
if ((lblQty != null))
{
lblQty.Text = DataBinder.Eval(e.Row.DataItem, "ODt_Amount").ToString();
}
//*** Total ***//
Label lblTotal = (Label)e.Row.FindControl("lblTotal");
if ((lblTotal != null))
{
strTotal = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ODt_Amount")) * Convert.ToDouble(dt.Rows[0]["Pro_PricePerUnit"]);
strSumTotal = Convert.ToDouble(strSumTotal) + strTotal;
lblTotal.Text = strTotal.ToString("#,###.00");
}
//*** Delete ***//
LinkButton lnkDelete = (LinkButton)e.Row.FindControl("lnkDelete");
if ((lnkDelete != null))
{
lnkDelete.Text = "Delete";
lnkDelete.CommandName = "Del";
lnkDelete.CommandArgument = e.Row.RowIndex.ToString();
lnkDelete.Attributes.Add("OnClick", "return confirm('Delete this row?');");
}
}
}
protected void lnkClearCart_Click(object sender, EventArgs e)
{
Session.Remove("myCart");
Response.Redirect("Viewcart.aspx");
}
protected void lnkCheckOut_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)Session["myCart"];
if (Session["myCart"] != null)
{
if (dt.Rows.Count > 0)
{
this.pnlCheckOut.Visible = true;
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
string str = null;
DataTable dt2 = null;
string strOrderID = "0";
int i = 0;
connect_db();
str = "insert into Orders (Order_Date,Cus_ID,Order_Name,Order_Address,Order_Tel,Order_PriceAll) " + " VALUES " + " (@sOrderDate,@sid,@sName,@sAddress,@sTel,@sss)";
SqlCommand insert = new SqlCommand(str, dbcon.conn);
insert.Parameters.AddWithValue("@sOrderDate", DateTime.Now.ToString("MM/dd/yyyy"));
insert.Parameters.AddWithValue("@sid", txtid.Text);
insert.Parameters.AddWithValue("@sName", txtName.Text);
insert.Parameters.AddWithValue("@sAddress", txtAddress.Text);
insert.Parameters.AddWithValue("@sTel", txtTel.Text);
insert.Parameters.AddWithValue("@sss", lblSumTotal.Text);
insert.ExecuteNonQuery();
str = "SELECT Max(Order_ID) As sOrderID FROM Orders ";
SqlDataAdapter da = new SqlDataAdapter(str, dbcon.conn);
DataTable dt1 = new DataTable();
da.Fill(dt1);
if (dt1.Rows.Count > 0)
{
strOrderID = dt1.Rows[0]["sOrderID"].ToString();
}
dt2 = (DataTable)Session["myCart"];
for (i = 0; i <= dt2.Rows.Count - 1; i++)
{
str = "INSERT INTO OrderDetail (Order_ID,Pro_ID,ODt_Amount,ODt_Price) " + " VALUES " + " ('" + strOrderID + "','" + dt2.Rows[i]["Pro_ID"] + "','" + dt2.Rows[i]["ODt_Amount"] + "')";
var _with1 = insert;
_with1.CommandText = str;
_with1.CommandType = CommandType.Text;
_with1.ExecuteNonQuery();
}
SqlCommand cmd = new SqlCommand("update [Orders] set Order_Status='please payment' where Order_ID=" + strOrderID + "", dbcon.conn);
cmd.ExecuteNonQuery();
dbcon.closeDB();
Session.Remove("myCart");
Response.Redirect("Vieworder.aspx?Order_ID=" + strOrderID);
}
}
ผมลองแก้ตรงส่วนนี้ดู
Code (ASP)
str = "INSERT INTO OrderDetail (Order_ID,Pro_ID,ODt_Amount,ODt_Price) " + " VALUES " + " ('" + strOrderID + "','" + dt2.Rows[i]["Pro_ID"] + "','" + dt2.Rows[i]["ODt_Amount"] + "','" + strTotal + "')";
ผลออกมาแบบนี้ครับ มันบันทึกราคารวมอันสุดท้ายลงไปอะครับ
รบกวนช่วบดูให้ผมทีนะครับ ขอบคุณครับ
Tag : .NET
|
ประวัติการแก้ไข 2012-10-17 18:43:40
|
|
|
|
|
Date :
2012-10-17 18:40:41 |
By :
alee62 |
View :
1228 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|