Solved

Listbox display member and tag data in vb.net

Posted on 2008-10-14
3
1,828 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

19 Experts available now in Live!

Get 1:1 Help Now