Solved

Moving past VB6 to Visual Studio 2010

Posted on 2010-11-10
6
365 Views
Last Modified: 2012-05-10
I'm finally leaving the VB6 world...have Visual Studio 2010 installed.  I've created a project and dropped some controls on it...just as an exercise, I want to populate my combo box from a command button click event.  Of course I'm getting a runtime error, "ArgumentException was unhandled" on the ComboBox1.DataSource = objReader line.  Do I have to reference something like I would in VB6?  How does it know which field I want to choose for the combo box?   I don't even know if I'm connected to the db.    Will some kind Expert help me take this first step?
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      
Dim strSQL As String

strSQL = ""
strSQL = "SELECT * FROM myDB.myOwner.myTable ORDER BY MyField"

'Establish connection and open database connection
Dim objConnection As New SqlClient.SqlConnection("server=mySQLServer;database=myDB;user=myUser;pwd=MyPWD")

'Establish command object
Dim objCommand As New SqlClient.SqlCommand(strSQL, objConnection)

Dim objReader As SqlClient.SqlDataReader

objConnection.Open()

objReader = objCommand.ExecuteReader

ComboBox1.DataSource = objReader

objReader.Close()

objConnection.Close()

End Sub
End Class

Open in new window

0
Comment
Question by:MariaHalt
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34105890
You can't use a DataReader as the datasource of the ComboBox as it only accepts objects that implement the IList or IListSource interfaces.

You can find a simple example here that uses a DataTable instead:

    http://msdn.microsoft.com/en-us/library/w67sdsex(v=VS.90).aspx

Or you could try this:
   '// you want to be careful about using * in select statements!
   Dim strSQL As String = "SELECT * FROM myDB.myOwner.myTable ORDER BY MyField"

   'Establish connection and open database connection
   Dim objConnection As New SqlClient.SqlConnection("server=mySQLServer;database=myDB;user=myUser;pwd=MyPWD")

   'Establish command object
   Dim objCommand As New SqlClient.SqlCommand(strSQL, objConnection)
   Dim adap As New SqlDataAdapter(objCommand, objConnection)

   Dim ds As New DataSet()
   adap.Fill(ds)

   '// tell the combobox which fields to use
   ComboBox1.DisplayMember = "Name of column for text display"
   ComboBox1.ValueMember = "Name of column for underlying value, probably an ID of some kind"

   ComboBox1.DataSource = ds.Tables(0)


End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:x77
ID: 34105952
Windows Form Contros DataReader do´nt support DataReader as DataSource.
You can do it with Web Controls.

For Windows App use a DataTable as DataSource:

Dim Dt as new DataTable
Dt.Load(objReader)
ComboBox1.DataSource =Dt

or

Dim Dt As new DataTable, Da as new SqlClient.SqlDataAdapter(strSQL, objConnection)
Da.Fill(Dt)
ComboBox1.DataSource = Dt

0
 

Author Comment

by:MariaHalt
ID: 34105988
Thanks for the quick reply, I'm getting a different error:  Type 'SqlDataAdapter' is not defined.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 34106029
you can use SqlCient.SqlDataAdapter
0
 

Author Comment

by:MariaHalt
ID: 34106205
Still getting an error:  Type 'SqlCient.SqlDataAdapter' is not defined.


'Dim adap As New SqlDataAdapter(objCommand, objConnection)
Dim adap As New SqlCient.SqlDataAdapter(objCommand, objConnection)
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34106307
You'll need to add the following at the top of your file, if you don't have it already:


     Imports System.Data.SqlClient
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
VB.Net Tranfer data between to VB.Net apps 8 44
Sending receiving text messages in vb.net 15 61
VB.Net Determine if a mapped network drive exists 2 52
ModalPopup  question 22 36
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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