Solved

Listbox display member and tag data in vb.net

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
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.…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 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