Reputation: 1571
I have a DataGridView
that is databound to a dataset. However I am trying to run a Query no the datasource and update the DataGridView
to display the Query Results.
The code I'm using is:
Public Sub Call_Filter()
Dim myCon = New OleDbConnection(My.Settings.Database_string)
Try
myCon.Open()
Catch ex As Exception
MsgBox("Sorry, An Error Occurred" & vbNewLine & _
"Database Not Found!" & vbNewLine & vbNewLine & _
"Error Message: " & ex.Message, MsgBoxStyle.OkOnly, "Could Not Locate Database")
Main.VIEW_SavingMessage.Visible = False
Exit Sub
End Try
Dim FilterAdaptor = New OleDbDataAdapter
Dim FilterAdaptorText = "SELECT ID, [Name Of Person], [SAP Job Number], [Site Name], [Asset Description], [Spares Supplier], [Supplier Contact Name], [Supplier Contact Phone Number], " & _
"[Supplier Contact Email], [Spares Description], [Part Number], [Quantity To Order], Cost, Comments, [Request Date], [Date Ordered], [Ordered By], [Invoice Received], " & _
"[Invoice Paid], [Method Of Payment], [Date Item Received], [Quote Attatchment], [Marked As Urgent] " & _
"FROM Spares " & _
"WHERE ([Name Of Person] = @NameOfPerson OR @NameOfPerson = '' OR @NameOfPerson IS NULL) AND " & _
"([SAP Job Number] = @SAPJobNo OR @SAPJobNo = '' OR @SAPJobNo IS NULL)"
Dim FilteredDataSet = New DataSet
Dim sqr As OleDbCommand = New OleDbCommand(FilterAdaptorText, myCon)
sqr.Parameters.Add("@NameOfPerson", OleDbType.VarChar).Value = Main.Name_Of_PersonToolStripTextBox.Text
If Main.FilterJobNo.Text <> "" Then
sqr.Parameters.Add("@SAPJobNo", OleDbType.Integer).Value = Main.FilterJobNo.Text
Else
sqr.Parameters.Add("@SAPJobNo", OleDbType.Integer).Value = DBNull.Value
End If
Try
sqr.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Sorry, An error occured!" & vbNewLine & _
"The database rejected the request" & vbNewLine & vbNewLine & _
"Error Message: " & ex.Message, MsgBoxStyle.OkOnly, "Database Refused Query")
Exit Sub
End Try
FilterAdaptor.SelectCommand = sqr
FilterAdaptor.Fill(FilteredDataSet)
Main.DataGridView1.DataSource = FilteredDataSet
myCon.Close()
End Sub
Whilst this code executes corretly, and throws no errors, it returns a blank row to the DataGridView
, not the correct filtered results. I know the filter query works correctly as I have tested it through Access and it returns the correct inormation.
I Can't use any other method that this as I have a large query to execute, this is just a fraction of the filter query for testing.
It appears that the issue I'm having is trying to get the filtered information in to my DataGridView
. Am I missing something basic? I've managed to insert, read and delete from databases in a similar method, but this doesn't seem to work when using a WHERE command.
UPDATE - 29/06/15
I'm still trying to get this to work, but after researching have changed my approach to this:
Public Function getDataSet() As DataSet
Dim ds As New Data.DataSet
ds.Tables.Add(New DataTable("Filtered"))
Dim Con As New OleDb.OleDbConnection(My.Settings.Database_string)
Con.Open()
Dim ad As New Data.OleDb.OleDbDataAdapter("SELECT ID, [Name Of Person] " & _
"FROM Spares " & _
"WHERE [Name Of Person] = Joe", Con)
ad.Fill(ds.Tables("Filtered"))
Return ds
End Function
Whats happening now is that I'm getting the following error:
No value given for one or more required parameters.
But I don't understand where this supposed "Parameter" has come from? I haven't used any parameters?
Thanks
Upvotes: 0
Views: 91
Reputation: 1571
I finally managed to get it going using this:
Public Function getDataSet() As DataSet
Dim ds As New Data.DataSet
ds.Tables.Add(New DataTable("Filtered"))
Dim Con As New OleDb.OleDbConnection(My.Settings.Database_string)
Con.Open()
Dim ad As New Data.OleDb.OleDbDataAdapter
Dim cmd As New OleDbCommand("SELECT * FROM Spares " & _
"WHERE ([Name Of Person] = @NOP OR [Name Of Person] = '' OR [Name Of Person] IS NULL Or @NOP IS NULL) AND " & _
"([SAP Job Number] = @SJN OR [SAP Job Number] = '' OR [SAP Job Number] IS NULL Or @SJN IS NULL)", Con)
If Main.Name_Of_PersonToolStripTextBox.Text = "" Then
cmd.Parameters.AddWithValue("@NOP", DBNull.Value)
Else
cmd.Parameters.AddWithValue("@NOP", Main.Name_Of_PersonToolStripTextBox.Text)
End If
If Main.FilterJobNo.Text = "" Then
cmd.Parameters.AddWithValue("@SJN", DBNull.Value)
Else
cmd.Parameters.AddWithValue("@SJN", Main.FilterJobNo.Text)
End If
ad.SelectCommand = cmd
ad.Fill(ds.Tables("Filtered"))
Return ds
End Function
and calling it using this:
Private Sub FILTER_Accept_Button_Click(sender As Object, e As EventArgs) Handles FILTER_Accept_Button.Click
DataGridView1.DataSource = Database_Management_Codes.getDataSet().Tables(0)
End Sub
Upvotes: 0
Reputation: 38895
There WHERE clause of your query is wrong.
"WHERE ([Name Of Person] = @NameOfPerson OR @NameOfPerson = '' OR @NameOfPerson IS NULL) AND
Since the parameter/variable is just one condition for [Name of Person]
. it should only appear once in the WHERE. If the name of the person is "Bob", that portion would resolve to:
"WHERE ([Name Of Person] = 'Bob' OR 'Bob'= '' OR 'Bob' IS NULL) AND ...
Clearly, "Bob" is never going to be Null or ''
. Those should all be the column name:
"WHERE ([Name Of Person] = 'Bob' OR [Name Of Person]= '' OR [Name Of Person] IS NULL) AND ...
The same would be true for the [SAP Job Number]
portion.
Upvotes: 1
Reputation: 2563
You don't seem to be binding your data. Try DataGridView1.Databind()
after you set the Datasource.
Upvotes: 0