Solved

Listbox display member and tag data in vb.net

Posted on 2008-10-14
3
1,817 Views
Last Modified: 2012-05-05
I want to display one field but tag to a different field in listbox control.
I know there are two properties of listbox control: DisplayMember and ValueMember.
But how can I use these to the following situation?
This is in a web application and the code behind is vb.net (Visual Studio 2008).
I am putting all data access codes in a class. Accordingly, I have a listbox control in one page.
The class name is DataAccess.vb.
Aspx page name is: default.aspx
                   default.aspx.vb                
I have the following in the default.aspx
<td>                    
<asp:ListBox ID="lstStoreRoom" runat="server" Rows="1"  SelectionMode="Multiple"></asp:ListBox>                                        
</td>  

In the DataAccess.vb I have the following:
Public Sub lstStoreroom(ByRef lstItemNo As ListBox)  'listview control of store room

        Try
            sqlConn = ConnectionManager.GetNewConnection()

            Using sqlConn
                Dim command As OracleCommand = New OracleCommand("SELECT STOREID, STORENAME " & _
                                                                 "FROM STOREROOM ORDER BY STOREID", sqlConn)

                sqlConn.Open()
                Dim reader As OracleDataReader = command.ExecuteReader()

                If reader.HasRows Then
                    Do While reader.Read()
                        lstItemNo.Items.Add(reader(1).ToString) 'want to display the store name
                    'want to tag the storeid for the storename because storeid will be used database manipulation.
                    Loop
                End If                
                reader.Close()
            End Using

        Finally
            If Not (sqlConn Is Nothing) Then
                sqlConn.Close()
            End If
        End Try
 End Sub        
 
 
 Then in the page load event of default.aspx.vb, I was doing the following:
 
 Dim obj As New DataAccess

 obj.lstStoreroom(lstStoreRoom)
 
 Can you help me how I can tag the StoreId in this case?
 
 Thanks a ton in advance,
 
 regards,
 Mamun
 
0
Comment
Question by:mamunahmed
3 Comments
 
LVL 7

Assisted Solution

by:Chandan_Gowda
Chandan_Gowda earned 20 total points
ID: 22713309
I have written the code in C#.Please convert it to VB.Net
class DataItem   

{   

   public string STOREID{get; set;}   

   public string STOREROOM {get; set;}   

}  
 

// set the display and value members - can be set at design time...   

lstItemNo.DisplayMember = "STOREROOM ";   

lstItemNo.ValueMember = "STOREID";   

while (reader.Read())   

{   

   DataItem item = new DataItem();   

   item.STOREID= reader["STOREID"].ToString();   

   item.STOREROOM = reader["STOREROOM "].ToString();   

   lstItemNo.Items.Add(item);   

}      

Open in new window

0
 
LVL 10

Accepted Solution

by:
GeoffSutton earned 30 total points
ID: 22713333
A better approach would be, rather than building the list items one at a time, to create a datatable, fill it, then use that table as the datasource.  Try this:
I want to display one field but tag to a different field in listbox control.
I know there are two properties of listbox control: DisplayMember and ValueMember.
But how can I use these to the following situation?
This is in a web application and the code behind is vb.net (Visual Studio 2008).
I am putting all data access codes in a class. Accordingly, I have a listbox control in one page.
The class name is DataAccess.vb.
Aspx page name is: default.aspx
                   default.aspx.vb                
I have the following in the default.aspx
<td>                    
<asp:ListBox ID="lstStoreRoom" runat="server" Rows="1"  SelectionMode="Multiple"></asp:ListBox>                                        
</td>  

In the DataAccess.vb I have the following:
Public Sub lstStoreroom(ByRef lstItemNo As ListBox)  'listview control of store room

        Try
            sqlConn = ConnectionManager.GetNewConnection()

            Using sqlConn
                Dim command As OracleCommand = New OracleCommand("SELECT STOREID, STORENAME " & _
                                                                 "FROM STOREROOM ORDER BY STOREID", sqlConn)

                'Dim reader As OracleDataReader = command.ExecuteReader()
                Dim dt as New DataTable()
                sqlConn.Open()
                dt.Load(command.ExecuteReader()
        Finally
            If Not (sqlConn Is Nothing) Then
                sqlConn.Close()
                lstItemNo.DataSource=dt
                lstItemNo.DataTextField="STORENAME "
                lstItemNo.DataValueField="STOREID"
                lstItemNo.DataBind()

            End If
        End Try
 End Sub        


What this will do is automatically add all the items to the listbox, and also set the storeID from each datarow to correspond with the storeName.  To retrieve the values you have to loop through each item in the listbox and check to see if it is selected, then you can pull the listitem.ValueMember from each list item that is selected.

Hope this helps,

Geoff
0
 

Author Closing Comment

by:mamunahmed
ID: 31505985
Actually, both the answers solved my problem. However, I used Geoff's one. Thanks a ton to both Chandan and Geoff.
Regards,
Mamun
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now