MariaHalt
asked on
Moving past VB6 to Visual Studio 2010
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
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(s trSQL, objConnection)
Da.Fill(Dt)
ComboBox1.DataSource = Dt
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(s
Da.Fill(Dt)
ComboBox1.DataSource = Dt
ASKER
Thanks for the quick reply, I'm getting a different error: Type 'SqlDataAdapter' is not defined.
you can use SqlCient.SqlDataAdapter
ASKER
Still getting an error: Type 'SqlCient.SqlDataAdapter' is not defined.
'Dim adap As New SqlDataAdapter(objCommand, objConnection)
Dim adap As New SqlCient.SqlDataAdapter(ob jCommand, objConnection)
'Dim adap As New SqlDataAdapter(objCommand,
Dim adap As New SqlCient.SqlDataAdapter(ob
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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:
Open in new window