i am taking the data from database and showing to combo box , i want to see the name but the original value is id.. i have one code here, please review it what i did the wrong here. Public Sub nameshow()
Try
'declare variables
Dim objDataAdapter As New SqlDataAdapter
Dim objDataAdapter1 As New SqlDataAdapter
Dim objDataSet As New DataSet()
Dim objDataSet1 As New DataSet()
'//state dataset of combo box
' Set the SelectCommand properties...
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = sql.SqlConn
objDataAdapter.SelectCommand.CommandText = "select * from tblBrand"
objDataAdapter.SelectCommand.CommandType = CommandType.Text
'//mention the second data
objdataadapter1.SelectCommand = New SqlCommand()
objDataAdapter1.SelectCommand.Connection = sql.SqlConn
objDataAdapter1.SelectCommand.CommandText = "select * from tblModel"
objDataAdapter1.SelectCommand.CommandType = CommandType.Text
' Open the database connection...
sql.SqlConn.Open()
' Fill the DataSet object with data...
objDataAdapter.Fill(objDataSet, "tblBrand")
objDataAdapter1.Fill(objDataSet1, "tblModel")
' Close the database connection...
sql.SqlConn.Close()
With (cboxBrandName)
.DataSource = objDataSet
.ValueMember = "tblBrand.BandID"
.DisplayMember = "tblBrand.BrandName"
End With
With (cboxModel)
.DataSource = objDataSet1
.ValueMember = "tblModel.ModelID"
.DisplayMember = "tblModel.ModelName"
End With
Catch ex As Exception
End Try
End Sub
after run the program nothing showing into the combo box. i dont understand why, and there is no error too.
i am calling this function in form load. thanks....
Here is what I think happens after studying it a bit more:
You are getting an error. Most likely "Connection is already open." Then this error is swallowed by you try catch statement. Thus never reaching the adding of the datasource to the combobox. Try this instead:
Public Function SelectRows( _
ByVal dataSet As DataSet, ByVal connectionString As String, _
ByVal queryString As String) As DataSet
Using connection As New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter()
adapter.SelectCommand = New SqlCommand( _
queryString, connection)
adapter.Fill(dataSet)
Return dataSet
End Using
End Function
Call it with this:
objDataSet = SelectRows(objDataSet, "ConnectionString","select * from tblBrand")
With (cboxBrandName)
.DataSource = objDataSet
.ValueMember = "tblBrand.BandID"
.DisplayMember = "tblBrand.BrandName"
End With
I do believe this will work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.