Update DataRow


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?
 
Ad

Advertisements

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?

No one to help me out?? I thought this must be a not-so-difficult to
answer question!
 
Ad

Advertisements

R

rn5a

No one to help me out?? I thought this must be a not-so-difficult to
answer question!- Hide quoted text -

- Show quoted text -

No offence intended but I swear that this newsgroup is the dumbest
newsgroup I have ever come across. I don't get answers for fundamental
questions even after 4-5 days! It's really strange. And I thought that
the ones answering questions in this post are ASP.NET experts, gurus &
MVPs.

I know it isn't mandatory for anyone to answer the posts that people
like me post in this newsgroup (rather any of the newsgroups) & that
people answering others' posts are doing it voluntarily for which they
aren't paid & I genuinely feel from the bottom of my heart that what
they are doing is highly praiseworthy more so because in todays world,
no one does anything for others without any personal gains but on some
occasions, I find it hard to digest that I don't get answers for even
simple & fundamental questions. It's too frustrating.

I have been using the Google archived newsgroups like ASP, SQL Server,
DataGrid, VB, IIS, Access, Win2K, WinXP etc.. since last 7-8 years &
have a lot of faith in it but never have I encountered anything like
this where my presumably simple posts have gone unanswered for days on
end.

Once again let me iterate that I don't intend to offend anyone.....now
just sit back & watch how many responses I get for this tirade.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DateTime DataType Mismatch 6
DataRow Delete Method 5
SqlCommand Parameters.Add 6
DataRow 1
DataRow? 1
Shopping Cart not updating until refresh 5
database not updating 0
Refresh Carrys out Click Code 2

Top