Syntax error: Missing operand after 'ID' operator - vb.net

i know theres loads of posts with this problem, but i cant seem to find the error in my code, any ideas?
heres my code:
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
On Error GoTo SearchErr
If txtSearch.Text = "" Then
Exit Sub
Else
Dim cantFind As String = txtSearch.Text
MainBindingSource.Filter = "(Convert(ID, 'System.String') LIKE '" & txtSearch.Text & "')" & _
"OR (Student ID LIKE '" & txtSearch.Text & "') OR (First Name LIKE '" & txtSearch.Text & "')" & _
"OR (Last Name LIKE '" & txtSearch.Text & "')"
If MainBindingSource.Count <> 0 Then
With dgvStudentInfo
.DataSource = MainBindingSource
End With
Im trying to search an Access DB in Vb.net

I think SQL is seeing "Student ID" as two words. If your table fields really have spaces in them you need to add brackets, "[Student ID]". (You may need single or double quote identifiers instead of brackets. Not sure.)

Related

SUMIFS for vb.net

Good day,
How can I do like sumifs in sql query in vb.net
Private Sub txtProcYM_TextChanged(sender As Object, e As EventArgs) Handles txtProcYM.TextChanged
Try
query = "SELECT SUM(prdInput) FROM ProdOutput WHERE HELPER ='" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';"
retrieveSingleResult(query)
proc1QTYIn.Text = dt.Rows(0)("prdInput").ToString()
Catch ex As Exception
End Try
End Sub
here is my code. The plan is to take the Sum of column prdInput based on the helper which is txtlot txtPartnumber and txprocess.
I managed to get display the first prdinput the query found but I need the the sum of its since I have multiple txtlot partnumber and process.
Thanks
Edit: I already did the SUM(prdInput) on my query but nothing shows on the textbox.
You're almost there. You just needed to alias the column that you performed the sum aggregate on:
Private Sub txtProcYM_TextChanged(sender As Object, e As EventArgs) Handles txtProcYM.TextChanged
Try
query = "SELECT SUM(prdInput) AS prdInput FROM ProdOutput WHERE HELPER ='" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';"
retrieveSingleResult(query)
proc1QTYIn.Text = dt.Rows(0)("prdInput").ToString()
Catch ex As Exception
End Try
End Sub
It might be easier to see the difference with a formatted query:
SELECT
SUM(prdInput) AS prdInput --you forgot to give this column a name
FROM ProdOutput
WHERE HELPER = '" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';

filtering datagridview data using "NOT LIKE" condition is unsuccessful.how to modify the code?

I am developing a simple library management system. i try to filter data with "NOT LIKE" condition. the following code which i use is working, no errors.But gives mixed results.when i filter only "Gunasena" in publisher data rows with Added_Date.but this gives Gnasena+godage datas.Added_Date filter is totally correct.Help me to solve this problem,Thanks. this code works 100% correct in "LIKE" condition.
Private Sub combobox4_TextChange(sender As Object, e As EventArgs) Handles combobox4.TextChange
Dim filters As New List(Of String)
If combobox1.Text = "Added Date" And withoutButton.Checked Then
filters.Add("[Added_Date] LIKE '" & combobox2.Text & "%' And [Author] NOT LIKE '" &
combobox4.Text & "%'")
End If
If combobox1.Text = "Added Date" And withoutButton.Checked Then
filters.Add("[Added_Date] LIKE '" &
combobox2.Text & "%' And [Publisher] not LIKE '" & combobox4.Text & "%'")
End If
BooksBindingSource.Filter = String.Join(" Or ", filters)
End Sub

Filter DataView by Numeric field

With my Table1 access I have 5 fields:
COMPANY_Id Numeric Type,
COMPANY_Ordre Numeric Type,
COMPANY_Total Numeric Type,
COMPANY_Name Text Type,
COMPANY_Date Text Type
When I make a filter by COMPANY_Total, COMPANY_Name, or COMPANY_Date, then the filter works. But with the same type Numeric of fields (COMPANY_Ordre) it does not work.
This is my code of filter dataView:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Try
Dim Dv_Filtre As DataView = DT.DefaultView
Dv_Filtre.RowFilter = "Convert( [COMPANY_Ordre], 'System.String') LIKE '" & TextBox1.Text & "%' OR Convert( [COMPANY_Total], 'System.String') LIKE '" & TextBox1.Text & "%' OR [COMPANY_Name] LIKE '" & TextBox1.Text & "%' OR [COMPANY_Date] LIKE '" & TextBox1.Text & "%'"
Catch ex As Exception
End Try
End Sub
Picture Of two numeric field
Try to add the % also before the textbox1 to get much more results not only data that ONLY ends with specific criteria, such as this:
Dv_Filtre.RowFilter="Convert([COMPANY_Ordre],'System.String') LIKE '%" & TextBox1.Text & "%' OR ...
I hope this can help you brother
^_^

How to use multiple BindingSource Filters to conduct a search using 2 different textboxes

I've been googling for several hours now trying to come up with a way around this. When I run the program, it works but doesn't filter both values, it just uses the one which is no good to me. I have come across several stack overflow threads but none of them quite meet my criteria.
Is there a way that these can be used together in order to help the user narrow down the search for a record?
Private Sub Searchbtn_Click(sender As Object, e As EventArgs) Handles Searchbtn.Click
Me.MainDBBindingSource3.Filter = "Surname ='" & TextBox1.Text & "'"
Me.MainDBBindingSource3.Filter = "DOB ='" & TextBox2.Text & "'"
Thanks in advance!
From the comments, this is how you could format your filter:
If TextBox2.Text = String.Empty Then
Me.MainDBBindingSource3.Filter = "Surname ='" & TextBox1.Text & "'"
Else
Me.MainDBBindingSource3.Filter = "Surname ='" & TextBox1.Text & "' AND DOB ='" & _
TextBox2.Text & "'"
End If
Also, see How to properly escape SQL when using BindingSource's Filter property and Apostrophe in DataView RowFilter

Populate ListView In VB.Net

Code to show the data in two tables display in ListView (CODE WORKS PERFECTLY):
#Region "FillListView"
Sub FillListview()
LV.Items.Clear()
myqry = "SELECT AccResult.StudNo,AccResult.CNumber,AccResult.FirstName,AccResult.LastName,AccResult.YrandSec,Exercises.Exer1,Exercises.Exer2,Exercises.Exer3,Exercises.Exer4,Exercises.Exer5 from AccResult INNER JOIN Exercises ON AccResult.StudNo = Exercises.StudNo ORDER BY AccResult.FirstName,AccResult.YrandSec Asc;"
mycmd = New OleDbCommand(myqry, con)
con.Open()
mydr = mycmd.ExecuteReader
While mydr.Read
With LV
.Items.Add(mydr("StudNo"))
With .Items(.Items.Count - 1).SubItems
.Add(mydr("CNumber"))
.Add(mydr("FirstName"))
.Add(mydr("LastName"))
.Add(mydr("YrandSec"))
.Add(mydr("Exer1"))
.Add(mydr("Exer2"))
.Add(mydr("Exer3"))
.Add(mydr("Exer4"))
.Add(mydr("Exer5"))
End With
End With
End While
con.Close()
End Sub
#End Region
Code for Populate ListView (ERROR):
Public Sub PopulateListView()
Me.LV.Items.Clear()
Dim OleDr As OleDb.OleDbDataReader
OleDr = OleDa.SelectCommand.ExecuteReader() <-----< ERROR: The specified field '[StudNo]' could refer to more than one table listed in the FROM clause of your SQL statement.
Do While OleDr.Read()
Dim Item As New ListViewItem
Item.Text = IIf(OleDr.IsDBNull(0), "", OleDr.Item(0))
For shtCntr = 1 To OleDr.FieldCount() - 1
If Not OleDr.IsDBNull(shtCntr) Then
Item.SubItems.Add(OleDr.Item("CNumber"))
Item.SubItems.Add(OleDr.Item("FirstName"))
Item.SubItems.Add(OleDr.Item("LastName"))
Item.SubItems.Add(OleDr.Item("YrandSec"))
Item.SubItems.Add(OleDr.Item("Exer1"))
Item.SubItems.Add(OleDr.Item("Exer2"))
Item.SubItems.Add(OleDr.Item("Exer3"))
Item.SubItems.Add(OleDr.Item("Exer4"))
Item.SubItems.Add(OleDr.Item("Exer5"))
Else
Item.SubItems.Add("")
End If
Next shtCntr
Me.LV.Items.Add(Item)
Loop
End Sub
Code for Search:
Private Sub BSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BSearch.Click
If txtSearch.Text = "" Then
MsgBox("Please enter keyword to search...", MsgBoxStyle.Information, "Keyword to search...")
txtSearch.Focus()
Exit Sub
End If
Call OpenConnection()
With OleDa
Call Initialized()
.SelectCommand = New OleDb.OleDbCommand()
.SelectCommand.CommandText = "SELECT * FROM [AccResult],[Exercises] WHERE StudNo Like '%%" & txtSearch.Text & "%%' or [YrandSec] Like '%%" & txtSearch.Text & "%%' or [LastName] Like '%%" & txtSearch.Text & "%%'" & _
"Or [FirstName] Like '%%" & txtSearch.Text & "%%' or [Exer1] Like '%%" & txtSearch.Text & "%%' or [Exer2] Like '%%" & txtSearch.Text & "%%' or [Exer3] Like '%%" & txtSearch.Text & "%%'" & _
"Or [Exer4] Like '%%" & txtSearch.Text & "%%' or [Exer5] Like '%%" & txtSearch.Text & "%%' ORDER By YrandSec, LastName ASC"
.SelectCommand.Connection = OleCn
Call PopulateListView()
If Me.LV.Items.Count >= 1 Then
MsgBox(Me.LV.Items.Count & " Record(s) found for " & "( " & Me.txtSearch.Text & " )", MsgBoxStyle.OkOnly, "Record(s) found...")
Else
MsgBox("No record(s) found for " & "( " & Me.txtSearch.Text & " )" & " , please try again... ", MsgBoxStyle.Critical, "No record found...")
txtSearch.Focus()
txtSearch.SelectAll()
End If
End With
Call CloseConnection()
End Sub
How to populate listview after searching data in two data tables. It gives me error after searching. Thank You in advance.
It seems that both tables AccResult and Exercises contain a field named StudNo and when you refer to that field in the where statement the database cannot decide for you which field you are referring to.
To remove the problem prefix the field name StudNo with the name of the table like you have done in the code that works
Said that, please consider that your query, using string concatenation, is doomed to failure.
If a simple single quote is present in your search term the code will fail with a syntax error.
And then there is the big problem of Sql Injection
With OleDa
Dim searchTerm = "%" & txtSearch.Text & "%"
Call Initialized()
.SelectCommand = New OleDb.OleDbCommand()
.SelectCommand.CommandText = "SELECT * FROM AccResult INNER JOIN Exercises " & _
"ON AccResult.StudNo = Exercises.StudNo " & _
"WHERE AccResult.StudNo Like ? " & _
"or [YrandSec] Like ? " & _
"or [LastName] Like ? " & _
"Or [FirstName] Like ? " & _
"or [Exer1] Like ? " & _
"or [Exer2] Like ? " & _
"or [Exer3] Like ? " & _
"Or [Exer4] Like ? " & _
"or [Exer5] Like ? " & _
"ORDER By YrandSec, LastName ASC"
.SelectCommand.Parameters.AddWithValue("#p1", searchTerm)
.SelectCommand.Parameters.AddWithValue("#p2", searchTerm)
.SelectCommand.Parameters.AddWithValue("#p3", searchTerm)
.SelectCommand.Parameters.AddWithValue("#p4", searchTerm)
.SelectCommand.Parameters.AddWithValue("#p5", searchTerm)
.SelectCommand.Parameters.AddWithValue("#p6", searchTerm)
.SelectCommand.Parameters.AddWithValue("#p7", searchTerm)
.SelectCommand.Parameters.AddWithValue("#p8", searchTerm)
.SelectCommand.Parameters.AddWithValue("#p9", searchTerm)
.SelectCommand.Connection = OleCn
.......
In this version I have removed the ugly string concatenation and used a parameterized query. The command is more readable and the join of the wildcard char is done just one time. Unfortunately, OleDb cannot recognize the parameters by their names and so we need to add a parameter (#pX) for every placeholder (?) present in the query text