How to Search a String with spaces in between - vb.net

i am new to VB. so when i run my program and search for the name . i want to search with any spaces in between. For example i want to search " Brian Tracy" like that
i am quite confused on the string function that would do that. Thank you for your help
This is the code am working on
Dim search As String = txtAuthorsName.Text
'Search Sql For Authors Data
Dim conn As New
OleDb.OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;Data Source=" +
Server.MapPath("~/Access/bookstore.accdb"))
conn.Open()
Dim sql As String = "SELECT AID, authorName, authorSex FROM Authors"
Dim cmd As Object
If searchAuthor.SelectedValue.Equals("Name") Then
If search.Length > 0 Then
Dim keywords As String() = search.Split(" ")
sql = "SELECT * FROM Authors where authorName like '%" & keywords(0) & "%'"
For k As Integer = 1 To keywords.Length - 1
sql += " or authorName Like '%" & keywords(k) & "%'"
Next
End If
cmd = New OleDb.OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("#authorName", "%" + txtAuthorsName.Text + "%")
Else
sql += "WHERE authorSex LIKE #authorSex"
cmd = New OleDb.OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("#authorSex", "%" + txtAuthorSex.Text + "%")
End If
Dim dbread = cmd.ExecuteReader()
GridView2.DataSource = dbread
GridView2.DataBind()
dbread.Close()
conn.Close()

This is an example on how to filter/search data :
Public Sub FilterData(valueToSearch As String)
'
Dim searchQuery As String = "SELECT * From Users WHERE CONCAT(fname, lname, age) like '%" & valueToSearch & "%'"
Dim command As New SqlCommand(searchQuery, connection)
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)
DataGridView1.DataSource = table
End Sub
Private Sub BTN_FILTER_Click(sender As Object, e As EventArgs) Handles BTN_FILTER.Click
FilterData(TextBox1.Text)
Make necessary changes, hope it works

Related

My SQL select query with condition using list in VB.net

I have a MySQL table (variable- exchange) with a column name "symbol" and I have a list of string named "listfinalselection". I want to select all rows where the list contains the cell value of column "symbol". But I got a error that there is an error in my SQL syntax. I can't understand that error. Please help. `
Dim mysqlconn As MySqlConnection
mysqlconn = New MySqlConnection
mysqlconn.ConnectionString = "server=localhost;user id=root;password=1234;database=Share"
mysqlconn.Open()
Dim sql As String = "SELECT * FROM " & exchange & " WHERE Symbol in (" & String.Concat(",", listfinalselection.Select(Function(i) $"'{i}'").ToArray()) & ");"
Dim adapter As New MySqlDataAdapter(sql, mysqlconn)
Dim datatable As New DataTable()
adapter.Fill(datatable)
DataGridView1.DataSource = datatable
DataGridView1.Refresh()
Dim ii As Integer = 0
For Each rw As DataGridViewRow In DataGridView1.Rows
Try
DataGridView1.Rows(ii).Cells(11).Value = listeleventh(listsymbol.IndexOf(DataGridView1.Rows(ii).Cells(3).Value.ToString))
Catch
End Try
ii = ii + 1
Next
End Sub `
Here's how we can properly and securely query MySQL for a list of values:
Dim con = New MySqlConnection("server=localhost;user id=root;password=1234;database=Share")
'NOTE WELL: you still have to make sure that exchange contains no SQL!
Dim cmd As New MySqlCommand("SELECT * FROM " & exchange & " WHERE Symbol in (", con)
For i as Integer = 0 to listfinalselection.Count - 1
cmd.Parameters.AddWithValue("#p" & i, listfinalselection(i))
cmd.CommandText &= "#p" & i & ","
Next i
cmd.CommandText = cmd.CommandText.TrimEnd(","c) & ")"
Dim adapter As New MySqlDataAdapter(cmd)
Dim dt as New DataTable
adapter.Fill(dt)
Please note, before someone quotes the "Can we stop using AddWithValue already" blog, it's irrelevant to mysql

Search between two dates in access database using sql

This is my code for search in access database 2010. My problem is that when I search between two datetimepicker the result is wrong in datagridview, I mean when I search from specific records between May and June it shows me records also from February.
Private Sub Search_Record()
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim sSQL As String = String.Empty
Dim bookdetials As New frmContactDetails
Try
'get connection string declared in the Module1.vb and assing it to conn variable
conn = New OleDbConnection(Get_Constring)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sSQL = "SELECT contact_id, first_name , birth_date, book_num, send_from, no_answer, no_answer_by, rec, book_answer_name, book_answer_num, send_date, send_to, project_name FROM tblAddressBook"
If CheckBox1.Checked = True Then
sSQL = sSQL & " where project_name like '%" & Me.TextBox2.Text & "%' " & _
" AND birth_date between '" & DateTimePicker1.Text & "' AND '" & DateTimePicker2.Text & "'"
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
Me.dtgResult.DataSource = dt
Label4.Text = dt.Rows.Count
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
datepicker text should be converted to datetime format in sql
I had the same problem, the solution was too silly but it worked
use text instead of datetime in the db
make sure the datetimepicker enters "short format" data

VB.NET SQL Filter doesnt show NULL

Sub KlantenFilterZoeken(ByVal DataGridI As DataGridView, ByVal Tbl As String, ByVal NaamStraatHuisnummerBusnummerPostcodePlaatsTelefoon As String, ByVal DBCON As String)
Dim Delen() As String = NaamStraatHuisnummerBusnummerPostcodePlaatsTelefoon.Split("|")
Dim objConnection As New SqlConnection(DBCON)
Dim objDt As New DataTable
DataGridI.DataSource = Nothing
objConnection.Open()
Dim sSQL As String = "SELECT * FROM " & Tbl & " Where " & "KlantID Like #KlantID AND naam Like #naam AND straat Like #straat AND huisnummer Like #huisnummer AND postcode LIKE #postcode AND plaats LIKE #plaats AND telefoon LIKE #telefoon"
Dim objCommand As SqlCommand = New SqlCommand(sSQL, objConnection)
'Parameters
objCommand.Parameters.AddWithValue("#KlantID", "%" & Delen(0) & "%")
objCommand.Parameters.AddWithValue("#naam", "%" & Delen(1) & "%")
objCommand.Parameters.AddWithValue("#straat", "%" & Delen(2) & "%")
objCommand.Parameters.AddWithValue("#huisnummer", "%" & Delen(3) & "%")
objCommand.Parameters.AddWithValue("#postcode", "%" & Delen(4) & "%")
objCommand.Parameters.AddWithValue("#plaats", "%" & Delen(5) & "%")
objCommand.Parameters.AddWithValue("#telefoon", "%" & Delen(6) & "%")
Dim objAdapter = New SqlDataAdapter(objCommand)
Dim objAdap As SqlDataAdapter = New SqlDataAdapter(sSQL, objConnection)
objAdapter.Fill(objDt)
DataGridI.DataSource = objDt
DataGridI.Columns(0).Visible = True
objConnection.Close()
objConnection.Dispose()
End Sub
This is my code to filter a datagridview, It works but the problem is telephone numbers that have value NULL are "hidden" by this code and I don't want this:
http://i.stack.imgur.com/VpXra.png
How can I fix this
I'm not sure what you would like to do exactly. But if you are trying to avoid empty cells, you could do something like this.
Dim objAdapter = New SqlDataAdapter(objCommand)
Dim objAdap As SqlDataAdapter = New SqlDataAdapter(sSQL, objConnection
objAdapter.Fill(objDt)
Dim i As Integer = objDt.Rows.Count
For i = 0 To objDt.Rows.Count Step 1
If (String.IsNullOrEmpty(objDt.Rows(i)("telefoon"))) Then
Dim phValue = "NULL"
objDt.Rows(i)("telefoon") = phValue
End If
Next
DataGridI.DataSource = objDt
DataGridI.Columns(0).Visible = True
objConnection.Close()
objConnection.Dispose()

Syntax error (comma) in query expression, header with commas

I am trying to check if value from label1 exist in dbf file column named : "NALOG,C,8". Header in DBF file I can not change, 'cause it represents column's format and field size. But with that I get this error : "Syntax error (comma) in query expression "NALOG,C,8 = #NAL"
Here is complete code :
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Dim FilePath As String = "C:\"
Dim DBF_File As String = "PROMGL"
Dim ColName As String = "NALOG,C,8"
'Dim SQLstr As String = "SELECT * FROM " & DBF_File
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _
" ;Extended Properties=dBASE IV;User ID=Admin;Password="
'cmd = New OleDbCommand("SELECT * FROM " & DBF_File)
cmd = New OleDbCommand("SELECT * FROM PROMGL WHERE " & ColName & " = #NAL")
cmd.Connection = con
con.Open()
cmd.Parameters.AddWithValue("#NAL", Label1.Text)
Using reader As OleDbDataReader = cmd.ExecuteReader()
If reader.HasRows Then
con.Close()
Label6.Text = "EXIST" & TextBox1.Text
TextBox1.Text = ""
TextBox1.Focus()
Else
Label6.Text = "DOESN'T EXIST"
End If
end using
Thanks.
If you have a column that is named this:
Dim ColName As String = "NALOG,C,8"
Then I would change it too this:
Dim ColName As String = "[NALOG,C,8]"
Use this instead:
Dim ColName As String = "NALOG"

OleDBException 0x80040e10 No value Given

The first part of this works correctly, when the Textbox value is null. It throws an exception however, when i try to filter search using the second part of the if statement.
The reason the table var is set that way is because the user has the option to load different tables.
The textbox string is the search parameter.
Public Class form1
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= .\Service.accdb ;")
Dim da As New OleDbDataAdapter
Private Sub getdata()
Me.Refresh()
Dim dt As New DataTable
Dim cmd As New OleDbCommand
Dim table As String = ComboBox1.Text.ToString
Dim txt As String = TextBox1.Text.ToString
If TextBox1.Text = Nothing Then
con.Open()
da.SelectCommand = New OleDbCommand("select * from [" & table & "] ;", con)
con.Close()
Else
con.Open()
da.SelectCommand = New OleDbCommand("select * from [" & table & "] where (External like '%" & txt & "%' or Internal like '%" & txt & "%' or Code like '%" & txt & "%');", con)
con.Close()
End If
Try
da.Fill(dt)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
DataGridView1.DataSource = dt
End Sub
End Class
Any help would be appreciated.
EDIT
It seems that this bit of code is stopping it from searching correctly, however without it i cannot save to the DB..
Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(da)
builder.QuotePrefix = "["
builder.QuoteSuffix = "]"
So i am assuming i am missing a paramter from this section?