Data type mismatch in criteria expression vb.net - vb.net

I am using VB.Net connecting to MS Access database. I think SQL syntax is wrong because before i add WHERE clause, it works. When i add WHERE clause, i got error ' data type mismatch in criteria expression'. Any help would be appreciated, thank you.
Here is my code
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught Where [OperationID] ='" & TextBoxOpID4.Text & "'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView2.DataSource = ds
Additional info: textboxOpID4 is enabled false

make something like this
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught Where [OperationID] =" & TextBoxOpID4.Text , myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView2.DataSource = ds
I quite use this kind of query when the where clause is a number because if you do put ' in a number column the output would be string which produces the error for example the ColumnID = 5 your code is like this ColumnID = '5' which is wrong especially if it is an AutoNumber field so do not insert a datatype number inside ' so it will not become a string

Related

Search by datagridview1 column1 values

i using this code to search by textbox to get the related data for the ID
Dim connetionString As String
Dim cnn As SqlConnection
connetionString="DataSource=IP;InitialCatalog=DB;UserID=sa;Password=password"
cnn = New SqlConnection(connetionString)
Dim com As String = "SELECT * FROM Table WHERE ID= '" & TextBox1.Text & "'"
Dim Adpt As New SqlDataAdapter(com, cnn)
Dim ds As New DataSet()
Adpt.Fill(ds, "Table")
DataGridView1.DataSource = ds.Tables(0)
What iam trying to do, i need to do the same code but with datagridview column1
Explaining:
i've already add button to upload data to datagridview the data include ID's
i need to search by those ID's in bulk, that included to the datagridview then post the income data to another datagridview,
Thanks in advance.
First, I think you should correct your existing code to use SQL Parameters.
Dim com As New SqlCommand("SELECT * FROM Table WHERE ID= #Id;", cnn)
com.Parameters.Add("#Id", SqlDbType.VarChar).Value = TextBox1.Text
Dim Adpt As New SqlDataAdapter(com)
Now to use the value in the first row, first column
Dim strId As String = DataGridView1.Rows(0).Cells(0).Value.ToString
Dim com2 As New SqlCommand("SELECT * FROM Table WHERE ID= #Id;", cnn)
com2.Parameters.Add("#Id", SqlDbType.VarChar).Value = strId
Dim Adpt2 As New SqlDataAdapter(com2)
I had to guess at the datatype. Please check your database to get the correct type. I guessed it was some type of string since you had it enclosed in single quotes.

SQL SELECT query not working with parameters, but works fine with concatenation

I'm trying to select the name of a customer based on the selected code from a ComboBox. When I run the following code, I get an error saying that there's no value for one or more required parameters.
sql = "SELECT [Customer_Name] FROM [Customers] WHERE [Customer_Code] = #code"
Dim cmd As New OleDb.OleDbCommand(sql, con)
cmd.Parameters.Add("#code", OleDb.OleDbType.VarChar).Value = cmbCustomer.Text
Dim da As New OleDb.OleDbDataAdapter(sql, con)
Dim ds As New DataSet
da.Fill(ds)
txtCustomer.Text = ds.Tables(0).Rows(0).Item("Customer_Name")
However, when I run the same query but without the parameter, it works fine.
sql = "SELECT [Customer_Name] FROM [Customers] WHERE [Customer_Code] = '" & cmbCustomer.Text & "'"
Dim cmd As New OleDb.OleDbCommand(sql, con)
Dim da As New OleDb.OleDbDataAdapter(sql, con)
Dim ds As New DataSet
da.Fill(ds)
txtCustomer.Text = ds.Tables(0).Rows(0).Item("Customer_Name")
Is there something really obvious that I've missed with the first way of doing it? If not, why is this way not working?
It's because of the OleDbDataAdapter
You're setting the DataAdapter to take the string of sql, and connection of con, which means the cmd.Parameter is not passed in with it.
So your code would be looking for da.SelectCommand.Parameters.Add.
You either need
da.SelectCommand.Parameters.Add("#Code", OleDb.OleDbType.VarChar).Value = cmbCustomer.Text
Or
Dim da As New OleDb.OleDbDataAdapter(cmd)

missing operator in query sum expression vb net

Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select sum[Shark Individual Weight] from FishCaught'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView1.DataSource = ds
myConnection.Close()
Hello. I am using vb.net connecting to ms access. I keep getting error 'missing operator in query expression'. I would like to know what's wrong with my sql statement.
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught Where ([OperationID]) = '" & TextBoxOpID4.Text & "'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView2.DataSource = ds
I think my SQL syntax is wrong. How should write it actually? I get this error 'Data type mismatch in criteria expression'. If i just write ' Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught', the sum will be display. So i think there's error in WHERE clause. Any help would be appreciated, thank you.
Sum is a function and you have a disturbing single quote, thus:
"Select Sum([Shark Individual Weight]) From FishCaught"
this is your code
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select sum[Shark Individual Weight] from FishCaught'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView1.DataSource = ds
myConnection.Close()
Supposed to be the code must be like this
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select sum(The_Name_Of_The_Field) from FishCaught'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView1.DataSource = ds
myConnection.Close()
Is that so, try this FINAL
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select sum(Shark__Individual_Weight) as FishWeight from FishCaught'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView1.DataSource = ds
myConnection.Close()
Try to change Shark Individual Weight to Shark_Individual_Weight it will work
"Select Sum([field name]) From FishCaught" should work. Have you checked the field name in database, it should not contain any space. To make field name more readable you can replace any spaces with "_".

Visual Basic - No value given for one or more required parameters

I have a DataGridView which displays the contents of a table (with sql query). But whenever I run the code, it displays this error:
No value given for one or more required parameters.
Where did it went wrong? The program ran without displaying any errors.
Here's the code for displaying in the DataGridView:
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BSPDatabase.accdb"
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from [Transactions] where transMonth=" & monthCombobox.Text & "", MyConn)
da.Fill(ds, "BSPDatabase") 'Change items to your database name
Dim view As New DataView(tables(0))
source1.DataSource = view
TransactionsDataGridView.DataSource = view
I think you are forming bad SQL. The value needs to be in quotes.
da = New OleDbDataAdapter(String.Format("Select * from [Transactions] where transMonth ='{0}'", monthCombobox.text), MyConn)

OLEDB - select Count not working

Find below what I've done so far, but unfortunately it's not working.
Private BS as New BindingSource
Dim ds As New DataSet
' Make the Connection
Using con As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = Database1.mdb")
con.Open()
Dim Sql = "SELECT COUNT ([Eventname]) FROM Eventproposal"
Dim da = New OleDb.OleDbDataAdapter(Sql, con)
da.Fill(ds, "Eventproposal")
' Set the Binding Source
bs.DataSource = ds.Tables("Eventproposal")
con.Close()
End Using
TextBox1.DataBindings.Add("Text", bs, "")
Couple things, you should end all SQL Commands to MS Access with ;
Dim Sql = "SELECT COUNT ([Eventname]) FROM Eventproposal;"
And you did not name your column which will give you an error when you attempt to access it by name.
Dim Sql = "SELECT COUNT ([Eventname]) AS Eventname FROM Eventproposal;"
I believe it will give it a name but not what your thinking. Lastly, when you do your binding, you will have to reference the name of the field in the table.
TextBox1.DataBindings.Add("Text", bs, "Eventname")