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>
LVL 13
davidlars99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

muzzy2003Commented:
When you pass multiple values of the same name, they create a single Request.Form collection item with the values concatenated with commas. You therefore need to split this into an array, and delete the rows individually. Make sure you do this in descending order of ItemID, i.e. bottom up, to prevent deleting the wrong rows.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davidlars99Author Commented:
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
0
davidlars99Author Commented:
I mean, I could not find logic
0
muzzy2003Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.