i m not using visual studio i m using notepad and visual basic
i need to filling a combo box with data store in table in database
anh my coding
Private sub form1_load(byval sender as system.object, byval e as system.eventargs)Handles MyBase.Load
dim conpubs as sqlconnection
dim cmdselect as sqlcommand
conpubs = new sqlconnection(“Data source=NINDSDBDEV1;Initial catalog=ITRS;integrated security=SSPI;persist security info=false;trusted_connection=yes”)
Try
cmdselect = new sqlcommand( “select * from tblvendorlookup”, conpubs)
conpubs.open()
Dim dt As New DataSet
dat.Fill(dt, “table”)
ComboBox1.DataSource = dt.Tables(“table”).DefaultView
ComboBox1.DisplayMember = “course_vendor_cd”
ComboBox1.ValueMember = “course_vendor”
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conpubs.close()
End Try
and i got error that the dataset not declared
Please help me fight with this 3 days
many thanks
IIRC you need a SqlDataAdaptor for this. I have not used it in ages, so I cannot remember exactly what to do. SqlDataAdaptor has a .Fill method that fills a dataset.
In C# you usually give the DataSet a name: DataSet ds = new DataSet(“ds”);
Y are you not using VS if I my ask? It will def make your life a lot easier
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim connString As String = (“Data source=NINDSDBDEV1;Initial catalog=ITRS;integrated security=SSPI;persist security info=false;trusted_connection=yes”)
Dim conn As New SqlConnection(connString)
’ fillComboBox1()
Dim strSQL As String = “Select * From tblvendorlookup”
Dim DA As New SqlDataAdapter(strSQL, conn)
Dim DS As New Dataset
DA .Fill(DS, “tblvendorlookup”)
'Create and populate the DataTable to bind to the ComboBox:
Dim dt As New DataTable
dt.Columns.Add(“Course_vendor_cd”, GetType(System.String))
dt.Columns.Add(“Course_vendor”, GetType(System.String))
’ Populate the DataTable to bind to the Combobox.
Dim drDSRow As DataRow
Dim drNewRow As DataRow
For Each drDSRow In DS.Tables(“tblvendolookup”).Rows()
drNewRow = dt.NewRow()
drNewRow(“course_vendor_cd”) = drDSRow(“course_vendor_cd”)
drNewRow(“course_vendor”) = drDSRow(“course_vendor”)
dt.Rows.Add(drNewRow)
Next
'Bind the DataTable to the ComboBox by setting the Combobox’s DataSource property to the DataTable. To display the “Description” column in the Combobox’s list, set the Combobox’s DisplayMember property to the name of column. Likewise, to use the “Code” column as the value of an item in the Combobox set the ValueMember property.
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
With ComboBox1
.DataSource = dt
.DisplayMember = “course_vendor”
.ValueMember = “course_vendor_cd”
.SelectedIndex = 0
End With
End Sub
Not 100% sure of the question, but you should simpilify it:
Dim conn As New SqlConnection(connString)
Dim strSQL As String = "SELECT * FROM TableName"
Dim da As New SqlDataAdapter(strSQL, conn)
Dim ds As New DataSet
da.Fill(ds, "TableName")
With ComboBox1
.DataSource = ds.Tables("TableName")
.DisplayMember = "ColumnName1"
.ValueMember = "ColumnName2"
.SelectedIndex = 0
End With
I dnt understand the code. Y are you filling a dataset, then Creating a new DataTable and looping through the dataset putting the values into a data table? Y not bind directly against the data set?
I would also suggest a SqlDataReader as apposed to a DataSet, as readers are a lot faster and it looks like there is no apparent reason that you have to use a DataSet.
Just create a data reader that selects all the columns you need, and then bind that to the combo box and then close it afterwards. And for performance, always select only the columns you need and not select *. As it will be a lat faster for 2 reasons:
You are returning more data bytes than required, which means more data needs to be transmitted over the wire.
Which is the BIGGEST reason not to use select *. Is that Select * prevents sql from using any indexes.