我有一个程序可以计算数据库中的总值,并将总值插入名为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 :(得分: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>")