[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

New ways to populate a listbox, vb.net

Posted on 2014-12-29
2
Medium Priority
?
764 Views
Last Modified: 2016-02-18
I have WSCGSoftwareDataSet.xsd with one table. I am looking to see how many solutions are there to populate a listbox with data like: "Select SoftwareID, Title From tblSoftware"

The solutions 1 and 2 are working fine. Can you come up with new ways of doing like solution 3 described below. Or maybe some other solution 4 or solution 5?

Solution 1 (works fine, included for reference):
     'create a table object
       Dim dt As New DataTable

       'create two columns
        dt.Columns.Add("SoftwareID", GetType(System.Int32))
        dt.Columns.Add("Title", GetType(System.String))

        ' add select columns from a table in the dataset to dt
        For Each r In WSCGSoftwareDataSet.Tables(0).Rows
            dt.Rows.Add(New Object() {r(0), r(1)})
        Next
       
        ' add dt to listbox datasource
        ListBox1.DataSource = dt

        ' identify display and value members of the listbox
        ListBox1.DisplayMember = "Title"
        ListBox1.ValueMember = "SoftwareID"

Open in new window


Solution 2 (works fine, included for reference):
        Dim results = (From r In WSCGSoftwareDataSet.Tables(0).AsEnumerable() _
                      Select New With { _
                          .SoftwareID = r.Field(Of Integer)("SoftwareID"), _
                          .Title = r.Field(Of String)("Title") _
                      }).ToList()

        ListBox1.DataSource = results

        '' identify display and value members of the listbox
        ListBox1.DisplayMember = "Title"
        ListBox1.ValueMember = "SoftwareID"

Open in new window


Question: Can you come up with Solution 3 described below or possibly with solution 4 or 5 you may have?

Solution 3: How to add a query via code to table adapter or data adapter (whichever makes sense) and use this query ("Select SoftwareID, Title From tblSoftware") to populate the list box. BTW, tblSoftware is the only table in the dataset WSCGSoftwareDataSet.

We can either put the returned result from this  query into a datareader and then add a new datatable (dt1) in dataset called WSCGSoftwareDataSet. And then assig dt1 to listbox datasource or as 4th solution use the datareader to populate the list box.

I gues dataadapter will have no role because noting is communicated with the database. A dataadapter is on play only when there is some activities are taking place between a dataset and a dataset. Here, to populate listbox the interaction doesn't extend beyond the dataset into database.

Your comment on this will be appreciated.

This is yet another solution idea possibly to be developed by you :
Public Class MyData
    Public Property SoftwareID As Integer
    Public Property Title As String
End Class

Open in new window


This is also easy one (it is done):
ListBox1.DataSource = WSCGSoftwareDataSet.Tables(0) 

'' identify display and value members of the listbox
ListBox1.DisplayMember = "Title"
ListBox1.ValueMember = "SoftwareID"

Open in new window

0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Assisted Solution

by:Ammar Gaffar
Ammar Gaffar earned 400 total points
ID: 40523500
In Solution 1 and Solution 2 You are doing unnecessary steps by creating new data table object in Solution 1 and copy data from your data set, and in Solution 2 you are casting data table to list.

The optimum solution you already done, the latest code:
ListBox1.DataSource = WSCGSoftwareDataSet.Tables(0) 

'' identify display and value members of the listbox
ListBox1.DisplayMember = "Title"
ListBox1.ValueMember = "SoftwareID"

Open in new window


But I didn't get the adapter section, you already used adapter to fill your data set (correct me if I am wrong). In this case if you have additional filtering in your data stored in data set you have two options.
Option 1 by customize sql statement at adapter level (re-fill data from your data source)
Option 2 by using data table views, by assigning filter string.

But again, it depends on your requirements, bellow questions can change your approach
Do you need to filter the data in your data set?
if yes how many expected records you will have in your data set? This can identify which is better from performance point of view to use views filter string or re-fill your data set from database.
0
 
LVL 34

Accepted Solution

by:
it_saige earned 1600 total points
ID: 40523665
There are a variety of ways to do this.  Here are some additional methods:

Form1.vb -
Imports System.Data.SqlClient

Public Class Form1
	Private ReadOnly conn As New SqlConnection(My.Settings.EE_Q28588127ConnectionString)
	Private ReadOnly querySource As New BindingSource
	Private ReadOnly entitySource As New BindingSource
	Private ReadOnly contextSource As New BindingSource
	Private command As SqlCommand

	Private Sub OnLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		' Using databinding.  Bound to ListBox1.
		TblSoftwareTableAdapter.Fill(DataSet.tblSoftware)

		' Using SQL DataReader with SQL Command.
		conn.Open()
		command = New SqlCommand("SELECT Title, SoftwareID FROM tblSoftware", conn)
		Dim reader As SqlDataReader = command.ExecuteReader
		Dim dt As New DataTable
		dt.Load(reader)
		querySource.DataSource = dt
		ListBox2.DataSource = querySource
		ListBox2.DisplayMember = "Title"
		ListBox2.ValueMember = "SoftwareID"

		' Using ADO.Net entity data model.
		Dim entityModel As New EE_Q28588127Entities
		entitySource.DataSource = entityModel.tblSoftwares
		ListBox3.DataSource = entitySource
		ListBox3.DisplayMember = "Title"
		ListBox3.ValueMember = "SoftwareID"

		' Using Linq 2 Sql data context.
		Dim context As New Linq2SqlDataClassesDataContext
		contextSource.DataSource = (From software In context.GetTable(Of tblSoftware2)() Select New With {software.Title2, software.SoftwareID2})
		ListBox4.DataSource = contextSource
		ListBox4.DisplayMember = "Title2"
		ListBox4.ValueMember = "SoftwareID2"
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.components = New System.ComponentModel.Container()
		Me.ListBox1 = New System.Windows.Forms.ListBox()
		Me.TblSoftwareBindingSource = New System.Windows.Forms.BindingSource(Me.components)
		Me.DataSet = New EE_Q28588127.DataSet()
		Me.ListBox2 = New System.Windows.Forms.ListBox()
		Me.ListBox3 = New System.Windows.Forms.ListBox()
		Me.ListBox4 = New System.Windows.Forms.ListBox()
		Me.Label1 = New System.Windows.Forms.Label()
		Me.Label2 = New System.Windows.Forms.Label()
		Me.Label3 = New System.Windows.Forms.Label()
		Me.Label4 = New System.Windows.Forms.Label()
		Me.TblSoftwareTableAdapter = New EE_Q28588127.DataSetTableAdapters.tblSoftwareTableAdapter()
		CType(Me.TblSoftwareBindingSource, System.ComponentModel.ISupportInitialize).BeginInit()
		CType(Me.DataSet, System.ComponentModel.ISupportInitialize).BeginInit()
		Me.SuspendLayout()
		'
		'ListBox1
		'
		Me.ListBox1.DataSource = Me.TblSoftwareBindingSource
		Me.ListBox1.DisplayMember = "Title"
		Me.ListBox1.FormattingEnabled = True
		Me.ListBox1.Location = New System.Drawing.Point(12, 36)
		Me.ListBox1.Name = "ListBox1"
		Me.ListBox1.Size = New System.Drawing.Size(260, 238)
		Me.ListBox1.TabIndex = 0
		Me.ListBox1.ValueMember = "SoftwareID"
		'
		'TblSoftwareBindingSource
		'
		Me.TblSoftwareBindingSource.DataMember = "tblSoftware"
		Me.TblSoftwareBindingSource.DataSource = Me.DataSet
		'
		'DataSet
		'
		Me.DataSet.DataSetName = "DataSet"
		Me.DataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema
		'
		'ListBox2
		'
		Me.ListBox2.FormattingEnabled = True
		Me.ListBox2.Location = New System.Drawing.Point(278, 36)
		Me.ListBox2.Name = "ListBox2"
		Me.ListBox2.Size = New System.Drawing.Size(260, 238)
		Me.ListBox2.TabIndex = 1
		'
		'ListBox3
		'
		Me.ListBox3.FormattingEnabled = True
		Me.ListBox3.Location = New System.Drawing.Point(12, 293)
		Me.ListBox3.Name = "ListBox3"
		Me.ListBox3.Size = New System.Drawing.Size(260, 238)
		Me.ListBox3.TabIndex = 2
		'
		'ListBox4
		'
		Me.ListBox4.FormattingEnabled = True
		Me.ListBox4.Location = New System.Drawing.Point(278, 293)
		Me.ListBox4.Name = "ListBox4"
		Me.ListBox4.Size = New System.Drawing.Size(260, 238)
		Me.ListBox4.TabIndex = 3
		'
		'Label1
		'
		Me.Label1.AutoSize = True
		Me.Label1.Location = New System.Drawing.Point(13, 13)
		Me.Label1.Name = "Label1"
		Me.Label1.Size = New System.Drawing.Size(98, 13)
		Me.Label1.TabIndex = 4
		Me.Label1.Text = "Using data binding:"
		'
		'Label2
		'
		Me.Label2.AutoSize = True
		Me.Label2.Location = New System.Drawing.Point(275, 13)
		Me.Label2.Name = "Label2"
		Me.Label2.Size = New System.Drawing.Size(190, 13)
		Me.Label2.TabIndex = 5
		Me.Label2.Text = "Using a data reader with SqlCommand:"
		'
		'Label3
		'
		Me.Label3.AutoSize = True
		Me.Label3.Location = New System.Drawing.Point(13, 277)
		Me.Label3.Name = "Label3"
		Me.Label3.Size = New System.Drawing.Size(166, 13)
		Me.Label3.TabIndex = 6
		Me.Label3.Text = "Using ADO.Net entity data model:"
		'
		'Label4
		'
		Me.Label4.AutoSize = True
		Me.Label4.Location = New System.Drawing.Point(275, 277)
		Me.Label4.Name = "Label4"
		Me.Label4.Size = New System.Drawing.Size(93, 13)
		Me.Label4.TabIndex = 7
		Me.Label4.Text = "Using Linq 2 SQL:"
		'
		'TblSoftwareTableAdapter
		'
		Me.TblSoftwareTableAdapter.ClearBeforeFill = True
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(547, 540)
		Me.Controls.Add(Me.Label4)
		Me.Controls.Add(Me.Label3)
		Me.Controls.Add(Me.Label2)
		Me.Controls.Add(Me.Label1)
		Me.Controls.Add(Me.ListBox4)
		Me.Controls.Add(Me.ListBox3)
		Me.Controls.Add(Me.ListBox2)
		Me.Controls.Add(Me.ListBox1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		CType(Me.TblSoftwareBindingSource, System.ComponentModel.ISupportInitialize).EndInit()
		CType(Me.DataSet, System.ComponentModel.ISupportInitialize).EndInit()
		Me.ResumeLayout(False)
		Me.PerformLayout()

	End Sub
	Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
	Friend WithEvents ListBox2 As System.Windows.Forms.ListBox
	Friend WithEvents ListBox3 As System.Windows.Forms.ListBox
	Friend WithEvents ListBox4 As System.Windows.Forms.ListBox
	Friend WithEvents Label1 As System.Windows.Forms.Label
	Friend WithEvents Label2 As System.Windows.Forms.Label
	Friend WithEvents Label3 As System.Windows.Forms.Label
	Friend WithEvents Label4 As System.Windows.Forms.Label
	Friend WithEvents DataSet As EE_Q28588127.DataSet
	Friend WithEvents TblSoftwareBindingSource As System.Windows.Forms.BindingSource
	Friend WithEvents TblSoftwareTableAdapter As EE_Q28588127.DataSetTableAdapters.tblSoftwareTableAdapter

End Class

Open in new window

Produces the following output -Capture.JPG
-saige-
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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