For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > February 2007 > Update DataRow









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Update DataRow
rn5a@rediffmail.com

2007-02-22, 10:08 pm

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?

rn5a@rediffmail.com

2007-02-24, 8:11 am

On Feb 23, 8:50 am, r...@rediffmail.com wrote:
> 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!

rn5a@rediffmail.com

2007-02-28, 7:10 pm

On Feb 24, 3:13 pm, r...@rediffmail.com wrote:
> On Feb 23, 8:50 am, r...@rediffmail.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com