?
Solved

Request.Form collection in VB.NET

Posted on 2004-11-24
4
Medium Priority
?
1,057 Views
Last Modified: 2008-03-03
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>
0
Comment
Question by:davidlars99
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
muzzy2003 earned 2000 total points
ID: 12663077
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
 
LVL 13

Author Comment

by:davidlars99
ID: 12665844
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
 
LVL 13

Author Comment

by:davidlars99
ID: 12665849
I mean, I could not find logic
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 12665923
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question