Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Listbox display member and tag data in vb.net

Posted on 2008-10-14
3
Medium Priority
?
1,880 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 80 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 120 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

926 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