?
Solved

.NET Listbox loop DB Update

Posted on 2009-04-23
10
Medium Priority
?
452 Views
Last Modified: 2013-11-26
Hi, how can I, upon a button click, loop through a listbox items and update my binded database like this: use the actual listbox index and update the DB field CATEGORY_ID. See my listbox code below. Code needed. Thanks
<asp:ListBox ID="lstDisplayOrder" runat="server" DataSourceID="odsOrderByDisplay" 
                                DataTextField="DESCRIPTION_E" DataValueField="CATEGORY_ID" 
                                Width="300px" SelectedValue='<%# Bind("CATEGORY_ID") %>' Rows="8"></asp:ListBox>

Open in new window

0
Comment
Question by:GPSAddict
  • 4
  • 3
  • 3
10 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24222351
it really not clear what functionality you are trying to achieve
it might be helpful if you can povide us with more details
0
 

Author Comment

by:GPSAddict
ID: 24223556
I need to code to loop through a listbox and then update a table using the listbox items index....
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24225740
i am still not clear what you want to achieve but you can loop a listbox as explained in the following example
http://stackoverflow.com/questions/380451/c-how-do-i-do-i-loop-through-items-in-a-list-box-and-then-remove-that-item
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 24225747
Do you want to update the database, based on some condition, like which items are selected in the ListBox?
0
 

Author Comment

by:GPSAddict
ID: 24225864
No....When I click on a button I want some code to loop through the listbox items and update a table...
field 1 : update with the listbox items index
where listbox DataValueField = SameValueinDB.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24227669
You have not indicated a programming language...
0
 

Author Comment

by:GPSAddict
ID: 24240951
VB.NET sorry.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1600 total points
ID: 24241505
Loop example:




        ' Get a reference to the table.
        Dim dt As DataTable = Me.DataSet1.Tables(0)
 
        ' Set the primary key to find Product ID values.
        dt.PrimaryKey = New DataColumn() {dt.Columns("ProductID")}
        For Each item As ListItem In Me.ListBox1.Items
            Dim productName As String = item.Text
            Dim productID As Integer = item.Value
 
            ' Find the row by Product ID primary key 
            Dim findRow As DataRow = dt.Rows.Find(productID)
 
            If findRow IsNot Nothing Then
                ' Update the product name.
                findRow("ProductName") = productName
            End If
 
        Next item

Open in new window

0
 

Author Comment

by:GPSAddict
ID: 24242257
How do I get the item index in the loop because this is the value I want to update with, not the text or value.
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 400 total points
ID: 24242339
modification of the loop as suggeted by BOB
this is just a quick hack and not the most elegent solution
better one will to just implement a for loop instead of a foreach loop

        Dim count as Int = 0
        For Each item As ListItem In Me.ListBox1.Items
            count = count + 1 ' count will give you the position in the list
            Dim productName As String = item.Text
            Dim productID As Integer = item.Value
 
            ' Find the row by Product ID primary key
            Dim findRow As DataRow = dt.Rows.Find(productID)
 
            If findRow IsNot Nothing Then
                ' Update the product name.
                findRow("ProductName") = productName
            End If
 
        Next item
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

830 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