Solved

Listbox display member and tag data in vb.net

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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