Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combobox using dataset

Status
Not open for further replies.

deva1

Programmer
Oct 28, 2002
27
US
Can anyone give me code to populate a combobox using dataset and sqldataadapter.I want to create the dataset and dataadapter through program not through the wizard.



Thanks alot

Deva
 
Here's one way using the Oledbdata adapter

dim ConnectionString as string = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=C:\Employee.mdb;"
Try

Dim strSQL As String = "SELECT EmployeeID, EmployeeName " & _
"FROM Employee"
Dim conn As OleDbConnection = New OleDbConnection(ConnectionString)
' The OledbDataAdapter is used to move data between SQL Server,
' and a DataSet.
detailAdapter = New OleDbDataAdapter(strSQL, ConnectionString)
conn.Open()
Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, conn)
Dim Reader As OleDbDataReader = myCommand.ExecuteReader()
combobox1.Items.Clear()
Do While Reader.Read
combobox1.Items.Add(Reader.Item("EmployeeName").ToString())
Loop
Reader.Close()
conn.Close()
myCommand.Dispose()
conn.Dispose()

Catch exc As Exception
' Unable to connect to SQL Server
MessageBox.Show("Error populating the Employee combobox.")


End Try
 

Use DataSource,ValueMember,DisplayMember property of ComboBox to bind with DataSet.

Imports System.Data.SqlClient

Dim myConnection As String
Dim myDA As SqlDataAdapter
Dim myDS As New DataSet()

Try
'Biuld the Connection & SQL string
myConnection = "Initial Catalog=pubs;Data Source=(local);User ID=xxx;password=xxx"

mySQL = "SELECT au_id, au_lname, au_fname FROM authors"

'Initialize the SqlDataAdapter with the SQL and Connection String,
'And then use the SqlDataAdapter to fill the DataSet with data.
myDA = New SqlDataAdapter(mySQL, myConnection)
myDA.Fill(myDS, "authors")

'Bind ComboBox to DataSet
ComboBox1.DataSource = myDS.Tables("authors")
ComboBox1.ValueMember = myDS.Tables("authors").Columns("au_id").ToString
ComboBox1.DisplayMember = myDS.Tables("authors").Columns("au_lname").ToString

Catch Excep As SqlClient.SqlException
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try




Email: pankajmsm@yahoo.com
 
Thankx alot.This is exactly what Iam looking for.


Great help


Deva
 
BTW, I just bought a Combo Box from DinkIT (out of the Xtras.NET catalog) and it looks VERY cool. It gives the ability to have multiple columns in the drop-down. From what they tell me, you will also be able to specify column headers and styles in the next version. And it was only $49.

Just thought everyone would like to know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top