|
-
Jun 14th, 2004, 10:30 PM
#1
Thread Starter
Lively Member
Combo Box
I am using VB.net with a SQL database. I have all the code to connect to the database. My combo Box is called cboItemNumber.
The table that Im pulling it from is called AUTO, FieldName that I want displayed in the combo box is called ItemNumber. What type of database code do I use to get this baby running?
-
Jun 14th, 2004, 11:08 PM
#2
Lively Member
VB Code:
cn = New SqlConnection("integrated security=true;initial catalog=northwind")
'connecting to northwind if northwind is the selected database
Dim cmdtext As String = "select * from categories"
'selecting * from categories table
Dim da As New SqlDataAdapter()
da.SelectCommand = New SqlCommand(cmdtext, cn)
Dim dt As New DataTable("Category Table")
da.Fill(dt)
With ComboBox1 'combobox used
.DataSource = dt
.DisplayMember = "CategoryName"
'field name you want to put to combo
End With
like this? if not, sorry...
-
Jun 14th, 2004, 11:25 PM
#3
Thread Starter
Lively Member
what does the (dt) refer too.
-
Jun 14th, 2004, 11:59 PM
#4
Lively Member
it's a datatable and get filled by the dataadapter... it then become a datasource where the combo gets info...
-
Jun 15th, 2004, 06:37 AM
#5
Thread Starter
Lively Member
ayan check this out. (or anyone else)
This is the code that I got. Justin is the database name, auto is the table that im looking in, and ItemNumber is what I want to put in the combo Box. Im using my local server group and my server name is JNASI. When I run this I get no errors but my combo box is not populated. Any advise?
Thanks
Imports System.data.sqlclient
Imports System.Data
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cn As New SqlConnection
Dim mycommand As New SqlCommand
Dim myDS As DataSet = New DataSet
Dim Mydatagrid As New DataGrid
Dim da As New SqlDataAdapter
Dim objConnection As SqlConnection = New SqlConnection("server=(JNASI);database=justin;user id=sa;password=")
Dim cmdtext As String = "select itemnumber from auto"
Dim dt As New DataTable("auto")
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboItemNumber.SelectedIndexChanged
'open the database connection
cn = New SqlConnection("integrated security=true;initial catalog=auto")
da.SelectCommand = New SqlCommand(cmdtext, cn)
da.Fill(dt)
With cboItemNumber
.DataSource = dt
.DisplayMember = "ItemNumber"
End With
End Sub
End Class
-
Jun 15th, 2004, 08:19 PM
#6
Lively Member
i guess it's something like this...
VB Code:
Dim cn As New SqlConnection()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.ConnectionString = "integrated security=true;initial catalog=justin"
cn.Open()
Dim cmdtext As String = "select * from [auto]"
Dim da As New SqlDataAdapter()
da.SelectCommand = New SqlCommand(cmdtext, cn)
Dim dt As New DataTable("Table Auto")
da.Fill(dt)
With ComboBox1
.DataSource = dt
.DisplayMember = "ItemNumber"
End With
End Sub
not in the selectedindexchanged event of your combo... hope this helps...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|