Link to home
Start Free TrialLog in
Avatar of davidlars99
davidlars99Flag for United States of America

asked on

Request.Form collection in VB.NET

when I delete only one record it's no problem, but if more then one %^$#@)!@$#%


just run the code and you'll see :)


<%@ Import Namespace="System.Data" %>
<%@ Page Language="VB" Debug="True" %>
<HTML>
<HEAD>
<Script Language="vb" Runat="Server">

Dim ds As DataSet(), _
      dt As DataTable, _
      dt2 As DataTable, _
      dc As DataColumn, _
      dr As DataRow, _
      PKColumn(1) As DataColumn
      
Public Sub Page_Load(s As Object, e As EventArgs)
      If Not IsPostBack Then
            BindData()
      End If
      
End Sub

Public Sub BindData()
      dt=New DataTable()
      dc=New DataColumn("ItemID", GetType(Integer))
      dc.AutoIncrement=True
      dc.AutoIncrementSeed=0
      dc.AutoIncrementStep=1
      dc.Unique=True
      dt.Columns.Add(dc)
      'PKColumn(0)=dc
      'dt.PrimaryKey=PKColumn
      
      dc=New DataColumn("Item Name", GetType(String))
      dt.Columns.Add(dc)
      
      dc=New DataColumn("Quantity", GetType(Integer))
      dt.Columns.Add(dc)
      
      dc=New DataColumn("Price", GetType(Decimal))
      dt.Columns.Add(dc)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 1"
      dr("Quantity")=1
      dr("Price")=19.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 2"
      dr("Quantity")=1
      dr("Price")=49.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 3"
      dr("Quantity")=1
      dr("Price")=69.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()      
      dr("Item Name")="Data Item 4"
      dr("Quantity")=1
      dr("Price")=19.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 5"
      dr("Quantity")=1
      dr("Price")=49.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 6"
      dr("Quantity")=1
      dr("Price")=69.99
      dt.Rows.Add(dr)      
      
      dr=dt.NewRow()      
      dr("Item Name")="Data Item 7"
      dr("Quantity")=1
      dr("Price")=19.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 8"
      dr("Quantity")=1
      dr("Price")=49.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 9"
      dr("Quantity")=1
      dr("Price")=69.99
      dt.Rows.Add(dr)            
      
      cart.DataSource=dt
      cart.DataBind()
      
      Dim total As Decimal
      total=dt.Compute("Sum(Price)", Nothing)
      lblmsg.Text="Order Total: " & total.ToString()
      
      Session("dt")=dt
End Sub

Public Sub dg_Paging(o As Object, e As DataGridPageChangedEventArgs)
      cart.CurrentPageIndex=e.NewPageIndex
      BindData()
End Sub

Public Sub Delete(s As Object, e As EventArgs)
      Dim item,arr
      Dim      i As Integer=0, _
            row As DataRow
            
      dt2=Session("dt")
      For Each item In Request.Form
            If item="ItemID" Then
                  dt2.Rows(Request.Form(item)).Delete()
            End If
      Next

      For Each row In dt2.Rows
            row("ItemID")=i
            i+=1
      Next
      cart.DataSource=dt2
      cart.DataBind()
      Session("dt")=dt2
End Sub

</Script>
<TITLE></TITLE>
</HEAD>
<BODY>

<form runat="server">
      <asp:datagrid id="cart" runat="server" autogeneratecolumns="false" width="300"
            allowpaging="true"
            pagesize="9"
            pagerstyle-mode="numericpages"
            onpageindexchanged="dg_Paging">
            <columns>
                  <asp:templatecolumn>
                        <itemtemplate>
                              <input type="checkbox" value="<%#Container.DataItem("ItemID")%>" name="ItemID">
                        </itemtemplate>
                  </asp:templatecolumn>
                  <asp:boundcolumn datafield="Item Name" headertext="Item Name" />
                  <asp:boundcolumn datafield="Quantity" headertext="Quantity" />
                  <asp:boundcolumn datafield="Price" headertext="Price" />
            </columns>
      </asp:datagrid>
      <br>
      <asp:label id="lblmsg" runat="server" />
      <br>
      <asp:button id="btn1" text="Delete" onclick="Delete" runat="server" />
</form>

</BODY>
</HTML>
ASKER CERTIFIED SOLUTION
Avatar of muzzy2003
muzzy2003

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of davidlars99

ASKER

got it, but there is one thing I don't understand why I must delete rows in descending order?

arr=Request.Form("ItemID")
arr=arr.split(",")

For i=UBound(arr) To 0 Step -1
         dt2.Rows(arr(i)).Delete()
Next
I mean, I could not find logic
Avatar of muzzy2003
muzzy2003

You are accessing the rows by index. If you delete in ascending order, then the indexes of the rows after the one you are on, and are still coming to, will change, so you'll be deleting the wrong rows.