0

Having trouble with a persistent error. My form allows the user to see a list of elements in a particular group. The input for group number is a combobox called groupbox and the output is a combobox called ElementResults. I am getting the error on the line: GroupSearch.ExecuteNonQuery()

Imports System.Data.OleDb
Public Class ElementsSearch
Public Shared connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data        Source=C:\Documents and Settings\A\My Documents\Visual Studio 2010\Projects\A2\Element.accdb;Persist Security Info=False;"
Public Shared ElementsTable

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

    Dim Search As New OleDbConnection(connectionString)
    Search.Open()

    Dim SearchCriteria As String
    SearchCriteria = Groupbox.Text
    Dim query As String = "SELECT * From ElementsTable WHERE Group='" & SearchCriteria & "'"
    Dim GroupSearch As New OleDbCommand(query, Search)
    GroupSearch.ExecuteNonQuery()
    Dim reader As OleDbDataReader = GroupSearch.ExecuteReader()
    ElementResults.Text = Convert.ToString(reader("Name"))

End Sub
End Class
4
  • Can you start with debugger and paste what is coming in SearchCriteria? Commented Mar 2, 2014 at 15:51
  • Why do you execute the command twice? Remove GroupSearch.ExecuteNonQuery(). Commented Mar 2, 2014 at 16:05
  • Im quite new to vb, and this was modified from a teachers example. I have removed the line you highlighted, but I still get an error: InvalidOperationException No data exists for the row/column. This error occurs on the line "elementresults.text = Convert.ToString(Reader("Name") Commented Mar 2, 2014 at 16:07
  • 1
    I'm not surprised, see my answer, but accept Patrick Hofman's answer as this solved you're initial question. Commented Mar 2, 2014 at 16:14

2 Answers 2

2

You define the field group which is a reserved keyword.

Try [group] instead like in this sample:

SELECT * From ElementsTable WHERE [Group]='aaa'
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh I should have thought of the reserved keywords. Your suggestion solved my error, but now I have a new one: InvalidOperationException No data exists for the row/column. This error occurs on the line "elementresults.text = Convert.ToString(Reader("Name"))
1

To avoid all the errors you're getting, consider changing your code to something like this:

Using connection As New OleDbConnection(connectionString)
    connection.Open()
    Using command As New OleDbCommand("SELECT * From ElementsTable WHERE [Group]=@Group", connection)
        command.Parameters.AddWithValue("@Group", SearchCriteria)
        Using reader As OleDbDataReader = command.ExecuteReader()
            Do While reader.Read()
                ElementResults.Text = reader.GetString("Name")
            Loop
        End Using
    End Using
End Using

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.