R
rn5a
When a DataGrid is in the editable mode, users can change the quantity
of the items that are currently in his shopping cart by entering a new
quantity in a TextBox. After changing the quantity, a column named
*Quantity* in a database table also gets updated with the changes made
by the user. The Form has a Label as well which shows the total cost
of all the items that are currently present in his cart. This the
OnUpdateCommand event handler of the DataGrid:
========================================
Sub UpdateCart(obj As Object, ea As DataGridCommandEventArgs)
Dim i As Integer
Dim iQty As Integer
Dim iTotal As Integer
Dim strSQL As String
Dim dTable As DataTable
Dim dRowTotal() As DataRow
Dim oleDbCmd As OleDbCommand
Dim oleDbConn As OleDbConnection
oleDbConn = New OleDbConnection(".........")
iQty = CType(ea.Item.Cells(4).Controls(0), TextBox).Text
strSQL = "UPDATE tblCart SET Quantity=" & iQty & " WHERE........"
oleDbCmd = New OleDbCommand(strSQL, oleDbConn)
oleDbConn.Open()
oleDbCmd.ExecuteNonQuery()
strSQL = "SELECT * FROM tblCart WHERE ......."
oleDbDapter = New OleDbDataAdapter(strSQL, oleDbConn)
dSet = New DataSet()
oleDbDapter.Fill(dSet, "Cart")
dTable = New DataTable
dTable = dSet.Tables("Cart")
dSet.Tables("Cart").AcceptChanges()
dRowTotal = dTable.Select(Nothing, "Total",
DataViewRowState.CurrentRows)
iTotal = 0
For i = 0 To dRowTotal.Length - 1
dTable.Rows(i).AcceptChanges()
Response.Write("Total: " & dRowTotal(i)("Total") & "<br>")
iTotal = iTotal + dRowTotal(i)("Total")
Next
lblTotal.Text = "TOTAL : " & iTotal & ".00"
dgCart.DataSource = dSet.Tables("Cart").DefaultView
'oleDbDapter.Update(dSet, "Cart")
oleDbConn.Close()
dgCart.EditItemIndex = -1
dgCart.DataBind()
End Sub
========================================
The above code successfully updates the *Quantity* column in the DB
table.
Assume that the total cost of all the items amounts to 1000. The user
then decides to update the quantity of an item (whose unit price is
200) from 1 to 3 which means that after updating the quantity, the
total cost should now be 1400 i.e. the Label should now display 1400
but the Label still retains the old value & wrongly displays the total
cost as 1000.
Where am I erring here?
of the items that are currently in his shopping cart by entering a new
quantity in a TextBox. After changing the quantity, a column named
*Quantity* in a database table also gets updated with the changes made
by the user. The Form has a Label as well which shows the total cost
of all the items that are currently present in his cart. This the
OnUpdateCommand event handler of the DataGrid:
========================================
Sub UpdateCart(obj As Object, ea As DataGridCommandEventArgs)
Dim i As Integer
Dim iQty As Integer
Dim iTotal As Integer
Dim strSQL As String
Dim dTable As DataTable
Dim dRowTotal() As DataRow
Dim oleDbCmd As OleDbCommand
Dim oleDbConn As OleDbConnection
oleDbConn = New OleDbConnection(".........")
iQty = CType(ea.Item.Cells(4).Controls(0), TextBox).Text
strSQL = "UPDATE tblCart SET Quantity=" & iQty & " WHERE........"
oleDbCmd = New OleDbCommand(strSQL, oleDbConn)
oleDbConn.Open()
oleDbCmd.ExecuteNonQuery()
strSQL = "SELECT * FROM tblCart WHERE ......."
oleDbDapter = New OleDbDataAdapter(strSQL, oleDbConn)
dSet = New DataSet()
oleDbDapter.Fill(dSet, "Cart")
dTable = New DataTable
dTable = dSet.Tables("Cart")
dSet.Tables("Cart").AcceptChanges()
dRowTotal = dTable.Select(Nothing, "Total",
DataViewRowState.CurrentRows)
iTotal = 0
For i = 0 To dRowTotal.Length - 1
dTable.Rows(i).AcceptChanges()
Response.Write("Total: " & dRowTotal(i)("Total") & "<br>")
iTotal = iTotal + dRowTotal(i)("Total")
Next
lblTotal.Text = "TOTAL : " & iTotal & ".00"
dgCart.DataSource = dSet.Tables("Cart").DefaultView
'oleDbDapter.Update(dSet, "Cart")
oleDbConn.Close()
dgCart.EditItemIndex = -1
dgCart.DataBind()
End Sub
========================================
The above code successfully updates the *Quantity* column in the DB
table.
Assume that the total cost of all the items amounts to 1000. The user
then decides to update the quantity of an item (whose unit price is
200) from 1 to 3 which means that after updating the quantity, the
total cost should now be 1400 i.e. the Label should now display 1400
but the Label still retains the old value & wrongly displays the total
cost as 1000.
Where am I erring here?