2014-10-05 75 views
0

顯示總價值我有一個統計數據庫中的總價值,並在一個名爲,txtCartItems.Text文本框插入總值的程序。 ,我遇到的問題是,每當我添加的Response.Write()的代碼塊,它顯示我在表中插入值,但它並沒有把我在txtCartItems.Text插入的總價值。當我刪除Response.Write()代碼塊時,它會在txtCartItems.Text中顯示計數值,但我無法查看最近在表中添加的值。我只想要的是,無論何時搜索一個項目並添加它,我都可以同時查看錶格中的值和插入到txtCartItems.Text中的值的總數。無法在文本框

Dim productQuantity As Integer 
Dim totalPrice As Integer 

Integer.TryParse(txtQuantity.Text, productQuantity) 
totalPrice = productQuantity * Val(txtSellingPrice.Text) 

'establish the first connection 
con = New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source = " & Server.MapPath("Erika_Kyl_PointOfSales.mdb")) 
con.Open() 
query2 = "INSERT INTO tbl_Orders(product_name,product_category,product_quantity,total_price) VALUES('" & txtProductName.Text & "','" & txtCategory.Text & "','" & productQuantity & "','" & totalPrice & "')" 
cmd = New OleDb.OleDbCommand(query2, con) 
cmd.ExecuteNonQuery() 
cmd.Dispose() 
con.Close() 

Response.Write(<script>alert('Orders Successfully Added!')</script>) 
Response.Write("<script>window.location.reload()</script>") 

'establish the second connection 
con = New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source = " & Server.MapPath("Erika_Kyl_PointOfSales.mdb")) 
con.Open() 
query3 = "SELECT COUNT(*) AS totalOrders FROM tbl_Orders" 
cmd = New OleDb.OleDbCommand(query3, con) 
dr = cmd.ExecuteReader() 
While dr.Read() 
    txtCartItems.Text = dr("totalOrders").ToString() 
End While 
'align the value of textbox in center 
txtCartItems.Style("text-align") = "center" 
cmd.Dispose() 
con.Close() 

'clear the items in the textbox after adding them to tbl_Orders 
txtISBN.Text = "" 
txtProductName.Text = "" 
txtCategory.Text = "" 
txtQuantity.Text = "" 
txtSellingPrice.Text = "" 
+0

不過我不清楚,但是,作爲一個建議,你可以用'如果dr.Read()',因爲這將是具有單值僅('COUNT(*)'),而不是用'做while' – 2014-10-05 06:14:07

+0

我要的是,每當我搜索項目,並添加它,它會自動添加到表,我可以看到在總價值表格在txtCartItems.Text中。 – 2014-10-05 06:17:41

+0

爲什麼你需要'Response.Write',如果重新加載,這是否Page_Load中設置在'txtCartItems'價值? – 2014-10-05 06:26:28

回答

0

我可以在這裏看到奇特,您是txtCartItems設置文本,你正在做Response.Write後,那確實之前重新載入網頁,即使你設置如果txtCartItems值。所以,你可以使用下面的代碼來設置的值,然後做reload

Dim productQuantity As Integer 
Dim totalPrice As Integer 

Integer.TryParse(txtQuantity.Text, productQuantity) 
totalPrice = productQuantity * Val(txtSellingPrice.Text) 

'establish the first connection 
con = New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source = " & Server.MapPath("Erika_Kyl_PointOfSales.mdb")) 
con.Open() 
query2 = "INSERT INTO tbl_Orders(product_name,product_category,product_quantity,total_price) VALUES('" & txtProductName.Text & "','" & txtCategory.Text & "','" & productQuantity & "','" & totalPrice & "')" 
cmd = New OleDb.OleDbCommand(query2, con) 
cmd.ExecuteNonQuery() 
cmd.Dispose() 
con.Close() 

'establish the second connection 
con = New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source = " & Server.MapPath("Erika_Kyl_PointOfSales.mdb")) 
con.Open() 
query3 = "SELECT COUNT(*) AS totalOrders FROM tbl_Orders" 
cmd = New OleDb.OleDbCommand(query3, con) 
dr = cmd.ExecuteReader() 
While dr.Read() 
    txtCartItems.Text = dr("totalOrders").ToString() 
End While 
'align the value of textbox in center 
txtCartItems.Style("text-align") = "center" 
cmd.Dispose() 
con.Close() 


'clear the items in the textbox after adding them to tbl_Orders 
txtISBN.Text = "" 
txtProductName.Text = "" 
txtCategory.Text = "" 
txtQuantity.Text = "" 
txtSellingPrice.Text = "" 

Response.Write(<script>alert('Orders Successfully Added!')</script>) 
Response.Write("<script>window.location.reload()</script>")