When I clear the search box in the form, the table shows the same info (I want the table to show the original one, without any query).
Code:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If TextBox14.Text = "" Then
Call NotFound()
Exit Sub
Else
CustomerInfo1BindingSource.Filter = "(Convert(ID, 'System.String') LIKE '" & TextBox14.Text & "')" &
"OR (CustomerName LIKE '" & TextBox14.Text & "') OR (CustomerNumber LIKE '" & TextBox14.Text & "')" &
"OR (OrderDate LIKE '" & TextBox14.Text & "')"
If CustomerInfo1BindingSource.Count <> 0 Then
With CustomerInfo1DataGridView
.DataSource = CustomerInfo1BindingSource
End With
Else
MsgBox("Not Found!")
CustomerInfoBindingSource.Filter = Nothing
End If
End If
End Sub
If I've read the question correctly it appears that you're saying this:
You add a search term to TextBox14 which is then applied to the CustomerInfo1BindingSource.Filter and in turn that filters CustomerInfo1DataGridView as expected
You remove the search term from TextBox14 and click the button again
The filtered CustomerInfo1DataGridView remains untouched rather than showing everything
If that is the case I can see that code flow isn't exactly what you're expecting. Change it to be something like:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If TextBox14.Text = "" Then
CustomerInfoBindingSource.Filter = Nothing
MsgBox("Not Found!")
Else
CustomerInfo1BindingSource.Filter = "(Convert(ID, 'System.String') LIKE '" & TextBox14.Text & "')" &
"OR (CustomerName LIKE '" & TextBox14.Text & "') OR (CustomerNumber LIKE '" & TextBox14.Text & "')" &
"OR (OrderDate LIKE '" & TextBox14.Text & "')"
If CustomerInfo1BindingSource.Count <> 0 Then
With CustomerInfo1DataGridView
.DataSource = CustomerInfo1BindingSource
End With
End If
End If
End Sub
As it stands with your code the lineCustomerInfoBindingSource.Filter = Nothing is not being hit because there is no search term in TextBox14. Instead it's calling this NotFound() method which we don't have visibility of, and then exiting the method.
It might be worth also reading up on the official documentation. You can call RemoveFilter on a BindingSource:
CustomerInfoBindingSource.RemoveFilter()
Related
Good Day! I'm clueless on visual studio. Please Help me.
I have a datagridview that has database access file.
its bound to textboxes
now i have a search button that has a textbox too.
it runs fine but it needs to input the complete name.
I want it to be when you just input a letter it shows all the names that has/have that letter.
for example if i type "A" all names in the the database that has letter "A" on it will show.
but in this case
if i type "AGING" it will say cant find. It must be "AGING OVEN" for it to show the result.
here is my code.
please help thanks
Private Sub SearchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchBtn.Click
On Error GoTo SearchErr
If SearchTxtbox.Text = "" Then
Exit Sub
Else
Dim cantFind As String = SearchTxtbox.Text
TOOLSANDEQUIPMENTBindingSource.Filter = "(CONVERT(ID, 'System.String') like '" & SearchTxtbox.Text & "')" & _
"OR ([TOOL and EQUIPMENT] like '" & SearchTxtbox.Text & "') OR ([QUANTITY] like '" & SearchTxtbox.Text & "')" & _
"OR ([ITEM NO] like '" & SearchTxtbox.Text & "')" & _
"OR ([PART NO] like '" & SearchTxtbox.Text & "')" & _
"OR ([DATE MODIFIED] like '" & SearchTxtbox.Text & "')"
If TOOLSANDEQUIPMENTBindingSource.Count <> 0 Then
With TEDataGridView
.DataSource = TOOLSANDEQUIPMENTBindingSource
End With
Else
MsgBox("--> " & cantFind & vbNewLine & _
"The search item was not found.", _
MsgBoxStyle.Information, "Hey Boss!")
TOOLSANDEQUIPMENTBindingSource.Filter = Nothing
With TEDataGridView
.ClearSelection()
.ReadOnly = True
.MultiSelect = False
.DataSource = TOOLSANDEQUIPMENTBindingSource
End With
End If
End If
ErrEx:
Exit Sub
SearchErr:
MsgBox("Error Number " & Err.Number & vbNewLine & _
"Error Description " & Err.Description, MsgBoxStyle.Critical, _
"Reser Error!")
Resume ErrEx
End Sub
I tried this code first before copying it to my main project (I created new project to test the codes first):
Private Sub posBtn_Click(sender As Object, e As EventArgs) Handles posBtn.Click
On Error GoTo wewe
If posText.Text = "" Then
Call notFound()
Exit Sub
Else
Dim cantFind As String = posText.Text
EmployeesRecordBindingSource.Filter = "(Convert(#_of_Employees, 'System.String') LIKE '" & posText.Text & "')" & "OR (last_name LIKE '" & posText.Text & "') OR (first_name LIKE '" & posText.Text & "')" & "OR (mi LIKE '" & posText.Text & "') OR (position LIKE '" & posText.Text & "')"
If EmployeesRecordBindingSource.Count <> 0 Then
With DataGridView2
.DataSource = EmployeesRecordBindingSource
End With
Else
MsgBox(cantFind & vbNewLine & "The search item was not found!", MsgBoxStyle.Information, "Hey boss")
EmployeesRecordBindingSource.Filter = Nothing
With DataGridView2
.ClearSelection()
.DataSource = EmployeesRecordBindingSource
End With
End If
End If
lul:
Exit Sub
wewe:
MsgBox("Error Number " & Err.Number & vbNewLine & "Error Description " & Err.Description, MsgBoxStyle.Critical, "Reset Error!")
Resume lul
End Sub
In my sample project, this is working. But when I copied it to my main project and run, I got this error: The expression contains invalid date constant '#_ofE_Employees, 'Sytem.String') LIKE '1')OR and so on... Did I missed something? Btw, it is for searching in datagridview. I have also private sub for reset, notfound.
Fields cannot start with the # as it is used for dates.
You might reconsider using signs and numbers when creating fields and variables in both your platform and database because there are chances those are reserved.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I don't know why when ever I click on the update button, I get an error
Syntax error in UPDATE statement
I have no idea what's going wrong in my code
This is my code:
Public Class Form1
Private Function vld(ByVal ParamArray ctl() As Object) As Boolean
For i As Integer = 0 To UBound(ctl)
If ctl(i).text = "" Then
ErrorProvider1.SetError(ctl(i), ctl(i).tag)
Return False
Exit For
End If
Next
Return True
End Function
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim da As OleDbDataAdapter
Dim dt As New DataTable
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
cn.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TxtExamtime.Format = DateTimePickerFormat.Custom
TxtExamtime.CustomFormat = "hh:MM tt"
cn.ConnectionString = "provider=microsoft.jet.oledb.4.0; Data Source=C:\psave\New folder\save.xls;Extended Properties=Excel 8.0;"
cn.Open()
FillDataGridView("select ID, Family Name, Given Name, Gender, DOB, Exam Date, Exam Time, Street Name, House Nr, PLZ, City from [edit$]")
End Sub
Private Sub FillDataGridView(ByVal Query As String)
da = New OleDbDataAdapter(Query, cn)
dt.Clear()
da.Fill(dt)
With DataGridView1
.DataSource = dt
.Columns(0).HeaderText = "ID"
.Columns(1).HeaderText = "Family Name"
.Columns(2).HeaderText = "Given Name"
.Columns(3).HeaderText = "Gender"
.Columns(4).HeaderText = "DOB"
.Columns(5).HeaderText = "Exam Date"
.Columns(6).HeaderText = "Exam Time"
.Columns(7).HeaderText = "Street Name"
.Columns(8).HeaderText = "House Nr"
.Columns(9).HeaderText = "PLZ"
.Columns(10).HeaderText = "City"
.Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
End Sub
Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
Try
FillDataGridView("select * from [edit$] where ID='" & TxtId.Text & "'")
TxtFamilyname.Text = dt.Rows(0).Item(1)
TxtGivenname.Text = dt.Rows(0).Item(2)
TxtGender.Text = dt.Rows(0).Item(3)
TxtDob.Text = dt.Rows(0).Item(4)
TxtExamdate.Text = dt.Rows(0).Item(5)
TxtExamtime.Text = dt.Rows(0).Item(6)
TxtStreet.Text = dt.Rows(0).Item(7)
TxtHouse.Text = dt.Rows(0).Item(8)
TxtPlz.Text = dt.Rows(0).Item(9)
TxtCity.Text = dt.Rows(0).Item(10)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
End Try
End Sub
Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
If vld(TxtId, TxtFamilyname, TxtGivenname, TxtGender, TxtDob, TxtExamdate, TxtExamtime, TxtStreet, TxtHouse, TxtPlz, TxtCity) = False Then
Exit Sub
Else
End If
Try
With cm
.Connection = cn
.CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "' )"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
Return
End Try
MsgBox("succefully Saved!", MsgBoxStyle.Information, Text)
End Sub
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
Try
With cm
.Connection = cn
.CommandText = "Update from [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where ID ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "' and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
Return
End Try
MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
End Sub
Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click
Close()
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
TxtId.Clear()
TxtFamilyname.Clear()
TxtGivenname.Clear()
TxtStreet.Clear()
TxtHouse.Clear()
TxtPlz.Clear()
TxtCity.Clear()
'To see all the data in DataGridView
FillDataGridView("select * from[edit$]")
End Sub
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
Try
With cm
.Connection = cn
.CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and ID ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
.ExecuteNonQuery()
End With
MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
End Try
End Sub
End Class
An Update statement does not have a "From" in it, so it should start with...
Update [Edit$]
There's a couple of other things wrong too. If the ID is a number then it probably doesn't need bounding single quotes, through they won't stop it either...
Where ID = " & txtId.text & "
Columns names that have embedded white space need bounding brackets...
and [Given Name] = '" & txtGivenName.text & "'
Finally, this statement is wide open to SQL Injection, where someone could do serious damage to your table by entering SQL into one of your text boxes. Please consider using parameters instead.
You should also consider using Microsoft.ACE.OLEDB.12.0 as the one you are using is quite old now.
You should probably have other parameters in your extended properties if you want to use Excel as a database, in particular you will need HDR=Yes...
Extended Properties=""Excel 8.0;HDR=Yes"""
This tells OLEDB that the first line of your sheet contains the column names, otherwise it will use F1...Fn (I think but it may be C1...Cn)
Beside some other issues with your code (e.g. you should almost always use parameterized queries), Update from [edit$] set... is wrong.
Just use Update [edit$] set....
Your Syntax for Update statment is clearly not correct :
please find below code :
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
Try
With cm
.Connection = cn
.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where ID ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "' and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]='" & TxtExamdate.Text & "'and [Exam Time]= '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr]= '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
Return
End Try
MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
End Sub
Place all column names inside [ ] other wise the query will be interpreted wrongly because it might ignore the rest of the query as soon as it encounters a white space
You have done it correctly for family name but certainly ignored other column names which have a space in between them
i am using vb.net and mysqladmin as my database.
i have a problem in my codes, and i don't know how to debug it.
please help me..
my problem is when i click the button update the error shows
"The CommandText property has not been properly initialized."
this is the codes:
Dim intDB_ID_Selected As Integer
'Private Sub cmdupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupdate.Click
If MessageBox.Show("Do you want to update this record?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
Dim sqlcommand As New MySqlCommand("UPDATE user_info " & _
" SET name = '" & txtname.Text.Trim & "'," & _
" address = '" & txtaddress.Text.Trim & "', " & _
" age = '" & txtage.Text.Trim & "', " & _
" WHERE id= '" & intDB_ID_Selected & "'", sConnection)
Call execCmd(SQL)
load1()
MsgBox("Record updated successfully.", MsgBoxStyle.Information)
End If
End Sub `
Public Sub execCmd(ByVal PstrSQL As String)
With cmd
.CommandText = PstrSQL
.ExecuteNonQuery()
End With
End Sub
the error line is in
.ExecuteNonQuery()
i am a beginner in this language, so please help me. im begging you guys!!
what is cmd and has it been initialized?
Oh, and try to used parameterized query. The earlier you get into the habit, the better.
Try doing this instead after the messagebox selection. cmd is not properly initialized in execCmd.
Dim sqlStr as String = "UPDATE user_info " & _
" SET name = '" & txtname.Text.Trim & "'," & _
" address = '" & txtaddress.Text.Trim & "', " & _
" age = '" & txtage.Text.Trim & "', " & _
" WHERE id= '" & intDB_ID_Selected & "'", sConnection)
Dim sqlcommand As New MySqlCommand(sqlStr)
sqlcommand.ExecuteNonQuery()
load1()
MsgBox("Record updated successfully.", MsgBoxStyle.Information)
what is missing in insert statement ? the full code is now here. i cant add new fields values to .mdb file. it took 3 days to develop this app and now its not running.
what is missing in insert statement ? the full code is now here. i cant add new fields values to .mdb file. it took 3 days to develop this app and now its not running.
Public Class client
Dim cnn As New OleDb.OleDbConnection
Private Sub reloaddata()
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("select * from clientsData", cnn)
Dim dt As New DataTable
da.Fill(dt)
Me.cview.DataSource = dt
cnn.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
display.Hide()
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = "provider= microsoft.jet.oledb.4.0; data source=" & Application.StartupPath & "\clients.mdb"
Me.reloaddata()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
display.Show()
End Sub
Private Sub cview_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles cview.CellClick
If cnn.State = ConnectionState.Closed Then
cnn.Open()
End If
Dim i As Integer
i = cview.CurrentRow.Index
If cview.CurrentCell.Value Is Nothing Then
MsgBox("Empty Field")
Else
view.lbl1.Text = cview.Item(0, i).Value.ToString
view.lbl2.Text = cview.Item(1, i).Value.ToString
view.lbl3.Text = cview.Item(2, i).Value.ToString
view.lbl4.Text = cview.Item(3, i).Value.ToString
view.lbl5.Text = cview.Item(4, i).Value.ToString
view.lbl6.Text = cview.Item(5, i).Value.ToString
view.lbl7.Text = cview.Item(6, i).Value.ToString
view.lbl8.Text = cview.Item(11, i).Value.ToString
view.lbl9.Text = cview.Item(12, i).Value.ToString
view.lbl10.Text = cview.Item(7, i).Value.ToString
view.lbl11.Text = cview.Item(8, i).Value.ToString
view.lbl12.Text = cview.Item(9, i).Value.ToString
view.lbl13.Text = cview.Item(10, i).Value.ToString
view.lbl14.Text = cview.Item(13, i).Value.ToString
view.Show()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
cmd.CommandText = "insert into clientsdata(ID,Client,Project,Domain,Hosting,bulk sms,maintenance,Order date,amount,last billing,next billing,username,password,due amount) VALUES ('" & Me.cid.Text & "','" & Me.cname.Text & "','" & Me.cproj.Text & "','" & Me.cdmn.Text & "','" & Me.chost.Text & "','" & Me.csms.Text & "','" & Me.cmain.Text & "','" & Me.codt.Text & "','" & Me.camnt.Text & "','" & Me.cldt.Text & "','" & Me.cndt.Text & "','" & Me.cuid.Text & "','" & Me.cpass.Text & "','" & Me.cdue.Text & "' )"
cmd.ExecuteNonQuery()
Me.reloaddata()
cnn.Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
cid.Text = "cid"
cname.Text = "cname"
cproj.Text = "cpro"
cdmn.Text = "domain"
chost.Text = "chost"
csms.Text = "sms"
cmain.Text = "main"
codt.Text = "codt"
camnt.Text = "mount"
cldt.Text = "last"
cndt.Text = "next"
cdue.Text = "due"
cuid.Text = "uid"
cpass.Text = "pass"
End Sub
End Class
ok problem solved !! I just added [ ] in the fields.
source: (VB.NET)Syntax Error in INSERT INTO statement - MICROSOFT JET DATABASE ENGINE
cmd.CommandText = "insert into clientsdata([ID],[Client],[Project],[Domain],[Hosting],[bulk sms],[maintenance],[Order date],[amount],[last billing],[next billing],[username],[password],[due amount]) VALUES (" & Me.cid.Text & ",'" & Me.cname.Text & "','" & Me.cproj.Text & "','" & Me.cdmn.Text & "','" & Me.chost.Text & "','" & Me.csms.Text & "','" & Me.cmain.Text & "','" & Me.codt.Text & "','" & Me.camnt.Text & "','" & Me.cldt.Text & "','" & Me.cndt.Text & "','" & Me.cuid.Text & "','" & Me.cpass.Text & "','" & Me.cdue.Text & "' )"
Your date field is a keyword. You have to place it with brackets: [date]. Also, any field names that have spaces require brackets, too: [Order date].
You really should use parameters to avoid SQL injection and to solve a host of other issues with updating databases.
I would also avoid trying to manage the connection state of the database. Just use the Using syntax so that the connection always closes.
If Me.cid.Text = "" Then
MessageBox.Show("Please input values")
Else
Using con As New OleDb.OleDbConnection("...")
con.Open()
Using cmd As New OleDb.OleDbCommand()
cmd.Connection = con
cmd.CommandText = "..."
cmd.Parameters.AddWithValue("#ID", Me.cid.Text)
cmd.Parameters.AddWithValue(...more)
cmd.ExecuteNonQuery()
End Using
End Using
reloaddata()
End If
It's hard to say what the problem is without knowing what is contained in each of the .Text values, and without seeing the definition of the table you are filling in. But I could hazard a guess that the Me.camnt.Text and Me.cdue.Text probably don't want to be surrounded by single quotes; that is to say, you may need something like this...
Me.codt.Text & "'," & Me.camnt.Text & ",'" & Me.cldt.Text
and
Me.cndt.Text & "'," & Me.cdue.Text & ",'" & Me.cuid.Text