ComboBox not filling with code - vb.net

I have the following code:
Using conn = New SqlConnection(connStr)
Dim sql = "SELECT [CATEGORIA], [AREA] FROM [CATEGORIAS] WHERE ([AREA] = #AREA)"
Dim sqlCmd = New SqlCommand(sql, conn)
sqlCmd.Parameters.AddWithValue("#AREA", CStr(PublicLogin.Area))
conn.Open()
Dim ds As New DataSet
Dim da As New SqlDataAdapter(sql, conn)
da.Fill(ds, "CATEGORIAS")
With cboCat
.DataSource = ds.Tables("CATEGORIAS")
.DisplayMember = "CATEGORIAS_AREA"
.ValueMember = "CATEGORIAS_AREA"
.SelectedIndex = 0
End With
End Using
It should in theory fill the combo box right? It doesn't. It doesn't give me any errors or anything; the combo box just sits there empty. I am pretty sure the query is working well and its grabbing the correct info but the Combobox doesn't fill. If I take the where query out it fills the CB with 'System.Data.Row...'
UPDATE: Working code in case someone needs it;
Using conn = New SqlConnection(connStr)
Dim sql = "SELECT [CATEGORIA], [AREA] FROM [CATEGORIAS] WHERE ([AREA] = #AREA)"
Dim sqlCmd = New SqlCommand(sql, conn)
sqlCmd.Parameters.AddWithValue("#AREA", CStr(PublicLogin.Area))
conn.Open()
Dim ds As New DataSet
Dim da As New SqlDataAdapter(sqlCmd)
da.Fill(ds, "CATEGORIAS")
With cboCat
.DataSource = ds.Tables("CATEGORIAS")
.DisplayMember = "CATEGORIA"
.ValueMember = "CATEGORIA"
.SelectedIndex = 0
End With
End Using

Its empty because probably the query returns nothing. Notice that you are not adding the parameter to the final query on the sql variable.
You create a command with the query and the parameter but you never use it, so when you select all the values WHERE ([AREA] = #AREA) it returns nothing.
Try this:
Dim da As New SqlDataAdapter(sqlCmd)

This looks odd to me:
.DisplayMember = "CATEGORIAS_AREA"
.ValueMember = "CATEGORIAS_AREA"
It should probably be something like this:
.DisplayMember = "CATEGORIA"
.ValueMember = "AREA"
From MSDN on DisplayMember:
Gets or sets a string that specifies the property or column from which to retrieve strings for display in the combo boxes.
EDIT: You also need to replace this:
Dim da As New SqlDataAdapter(sql, conn)
with this:
Dim da As New SqlDataAdapter(sqlCmd)
Otherwise your parameters are never used.

Related

How do i show my data from a table with specific position to a combobox? example is this

How do i show my data from a table in database with specific position to a combobox? example is this
Example
Only with the President Position candidates will appear on combobox1 from table1
Only with the VPresident Position candidates will appear on combobox2 from table1
and so on...
im stuck with this on combobox1 which is called prescombo
code
Private Sub prescombo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles prescombo.SelectedIndexChanged
Try
Dim query As String = "SELECT * FROM Candidates WHERE Position = President"
Using con As SqlConnection = New SqlConnection("Data Source=EXPOOKY;Initial Catalog=SchoolDB;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand(query, con)
Dim datapter As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim datable As DataTable = New DataTable()
datapter.Fill(datable)
prescombo.DisplayMember = "CandidateName"
prescombo.ValueMember = "CandidateID"
If datable.Rows.Count > 0 Then
If datable.Rows(3)("Position") = "President" Then
presparty.Text = datable.Rows(3).Item("Position").ToString
End If
End If
End Using
End Using
Catch ex As Exception
End Try
End Sub
This is what i tried based on my logic and its not working
If you're filtering the data using a WHERE clause in the query then there's no need to check the records in the result set. Just populate the DataTable and bind it.
Dim query = "SELECT * FROM Candidates WHERE Position = 'President'"
Dim connectionString = "Data Source=EXPOOKY;Initial Catalog=SchoolDB;Integrated Security=True"
Using adapter As New SqlDataAdapter(query, connectionString)
Dim table As New DataTable
adapter.Fill(table)
With prescombo
.DisplayMember = "CandidateName"
.ValueMember = "CandidateID"
.DataSource = table
Endf With
End Using

VB.net System.Data.OleDb.OleDbException: 'Syntax error (missing operator)

I am using Access 2013 as database in Visual studio 2017. The database is connected and all but when i run this code:
myConnToAccess = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database11.accdb")
myConnToAccess.Open()
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("SELECT Typ záznamu from Typ", myConnToAccess)
da.Fill(ds, "Typ")
Dim view1 As New DataView(tables(0))
With ComboBox1
.DataSource = ds.Tables("Typ")
.DisplayMember = "Typ záznamu"
.ValueMember = "Typ záznamu"
.SelectedIndex = 0
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
I get the **System.Data.OleDb.OleDbException: 'Syntax error (missing operator) in query expression 'Typ záznamu'.'
**
I have the
Dim mySQLCommand As OleDbCommand
Dim mySQLStrg As String
Dim ds As DataSet
Dim da As OleDbDataAdapter
Dim tables As DataTableCollection
Dim myConnToAccess As OleDbConnection
in the begining of the class... also the:
Imports System.Data.OleDb
I dont know what to d any more... can someone please help me?
If you want to query the column Typ záznamu then you need to put it in square brackets because of the space between the words.
da = New OleDbDataAdapter("SELECT [Typ záznamu] from Typ", myConnToAccess)
I had made it work:
this line:
da = New OleDbDataAdapter("SELECT Typ záznamu from Typ", myConnToAccess)
needs to look like this:
da = New OleDbDataAdapter("SELECT [Typ záznamu] from Typ", myConnToAccess)
The only difereance is in the column name. it needs to be in squere breaces or how do you call it [ ]

how to pull the data from database to combo box

i am trying to pull the data from database to combo box , i have 2 combo box and i want to display data from different tables.
this is the code i was trying.
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
in this line not showing any error but i cannot find any result in combo box.
Here's how I do it -
Dim ds As New DataSet()
Dim conn As New SqlConnection(yourconnectionString)
Const qry As String = "SELECT * FROM tblModel"
conn.Open()
Using da As New SqlDataAdapter(qry, conn)
da.Fill(ds)
End Using
' For Combobox
With YourComboBoxName
.DisplayMember = "what you want to display to the user in combobox"
.ValueMember = "ID associated with each record"
.DataSource = ds.Tables(0)
.SelectedIndex = 0
End With
conn.Close()
Hope this helps.

taking the data from database showing to combo box but the value member is different

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.

Showing data in textbox

I am trying to get data from a database (which I have done and know that works)
However I want to push the data into a variable.
I can not see what is wrong, at the moment there is a mistake around "cmd.select" so if someone could point me in the right direction that would be great!
Dim cn As SqlConnection = New SqlConnection()
Dim cmd As SqlCommand = New SqlCommand()
Dim sqladp As New SqlDataAdapter()
Dim ds As New DataSet()
cmd.Parameters.Clear()
cn.ConnectionString = ConfigurationManager.ConnectionStrings("PemcoConnectionString").ConnectionString
cmd.Connection = cn
GridView2.Visible = True
cmd.Connection = cn
cmd.CommandText = "spUserResultsDetails"
cmd.CommandType = CommandType.StoredProcedure
Session.Item("ID") = (sender.SelectedValue.ToString)
cmd.Parameters.AddWithValue("#ID", Session.Item("ID"))
For Each datarow As Data.DataRowView In cmd.Select(DataSourceSelectArguments.Empty)
sEmailAddress = datarow("UserEmail")
Next
sqladp.SelectCommand = cmd
sqladp.Fill(ds)
There are many things that need improvement, this should work:
Using cn = New SqlConnection(ConfigurationManager.ConnectionStrings("PemcoConnectionString").ConnectionString))
Using da = New SqlDataAdapter("spUserResultsDetails", cn)
da.SelectCommand.CommandType=CommandType.StoredProcedure
da.SelectCommand.Parameters.AddWithValue("#ID", CInt(Session.Item("ID")))
Dim table = New Data.DataTable()
da.Fill(table)
GridView2.DataSource = table
GridView2.DataBind()
End Using
End Using
A summary:
use the using statement always to ensure that disposable objects are getting disposed (closed) even on error
use a SqldataAdapter if you want to fill a DataTable, you don't need to open/close it with fill, use it's SelectCommand property to get a reference to the SqlCommand
set the table as DataSource of your GridView and DataBind it
if you use AddWithValue you should cast the passed objects to the correct type, otherwise the correct type cannot be inferred