8 Replies - 1700 Views - Last Post: 13 December 2010 - 03:24 AM Rate Topic: -----

#1 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

Searching through a datagrid

Posted 12 December 2010 - 11:04 PM

I really need some help. I am trying to perform a search and this is as far as I got. I am trying to search through a datagrid for a match either by last name or ID. I know the ID search is almost working but not sure how to make it look for whats in the text box... same for last name please help! I am not sure how to match the value in the search text to the datagrid.

Thanks



   Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        Dim ind As String = 0
        ' Dim Result As String
        Dim SearchArgument As String

        SearchArgument = Me.SearchTextBox.Text

        
        Dim cmd As OleDbCommand 'Object stores the SQL command text and connection info


        'the SQL Command
        'Instantiate a new OleDbCommand object using the CommandString value
        'and the ConnectionString value of the PersonalTableAdapter object.


        With EmployeeTableAdapter
            If .Connection.State = ConnectionState.Closed Then
                .Connection.Open()
                Debug.WriteLine("Conn: WAS CLOSED, NOW OPEN")
            End If

            If LastNameRadioButton.Checked = True Then
                Dim cmdString As String = "SELECT * FROM Employee WHERE Last Name = SearchArgument"
                cmd = New OleDbCommand(cmdString, .Connection)  'I am going off of your lab 10. Sorry I am having so
                TheReader = cmd.ExecuteReader() 'Now retrieve the resulting rows

            End If
            If IDRadioButton.Checked = True Then
                Dim cmdString As String = "SELECT * FROM Employee WHERE ID = '3'" ' if you can figure out how to pass a variable here
                cmd = New OleDbCommand(cmdString, .Connection)
                TheReader = cmd.ExecuteReader() 'Now retrieve the resulting rows

            End If
            'cmd = New OleDbCommand(cmdString, .Connection)  'I am going off of your lab 10. Sorry I am having so much trouble...
            If TheReader.HasRows() Then
                Do While TheReader.Read()

                   
                    With Me.SearchResultsListBox.Items

                        .Add(String.Format("{0,-3} {1,-10} {2,-15} {3,10} {4,20}, {5,-2} {6,-15} {7,-10}", TheReader("ID"), TheReader("First Name"), _
                                                                           TheReader("Last Name"), TheReader("Phone Number"), _
                                                                           TheReader("Comments"), TheReader("Status"), TheReader("Picture"), _
                                                                           TheReader("File Name")))
                    End With
                Loop

                Debug.WriteLine("AFTER ExecuteReader()")
            Else
                MessageBox.Show("No Rows returned by QUERY! ")
                                
            End If

        End With
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Searching through a datagrid

#2 NavilShah  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 50
  • Joined: 02-November 10

Re: Searching through a datagrid

Posted 13 December 2010 - 12:42 AM

Write select Query as follows.

Dim cmdString As String = "SELECT * FROM Employee WHERE Last Name ='"& SearchArgument &"'"



in both the places...

or either use

Dim cmdString As String = "SELECT * FROM Employee WHERE Last Name ='"& Me.SearchTextBox.Text &"'"


to save memory of a variable

and reply me whats the result....
Was This Post Helpful? 0
  • +
  • -

#3 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

Re: Searching through a datagrid

Posted 13 December 2010 - 12:53 AM

View PostNavilShah, on 12 December 2010 - 11:42 PM, said:

Write select Query as follows.

Dim cmdString As String = "SELECT * FROM Employee WHERE Last Name ='"& SearchArgument &"'"



in both the places...

or either use

Dim cmdString As String = "SELECT * FROM Employee WHERE Last Name ='"& Me.SearchTextBox.Text &"'"


to save memory of a variable

and reply me whats the result....

I got an error on this line and not very familiar with this error.
TheReader = cmd.ExecuteReader()
Syntax error (missing operator) in query expression 'Last Name ='3''.
Was This Post Helpful? 0
  • +
  • -

#4 NavilShah  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 50
  • Joined: 02-November 10

Re: Searching through a datagrid

Posted 13 December 2010 - 12:58 AM

In your Id radio button part change Last Name to ID..

Hope it will solve your error...
Was This Post Helpful? 0
  • +
  • -

#5 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

Re: Searching through a datagrid

Posted 13 December 2010 - 01:02 AM

View PostNavilShah, on 12 December 2010 - 11:58 PM, said:

In your Id radio button part change Last Name to ID..

Hope it will solve your error...


Yah sorry man I relized that now. It is very late over here. I have been working on this for along time now. Now that the ID part is done the Last name search should be the same process, but last name field, correct?



I did the same process as the ID but for the last name search part. but I recieved the error I did that I stated above. This time it is searching the right field though.

Attached image(s)

  • Attached Image

This post has been edited by ayabrego: 13 December 2010 - 01:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

Re: Searching through a datagrid

Posted 13 December 2010 - 01:34 AM

Here is a screen shotAttached Image
Was This Post Helpful? 0
  • +
  • -

#7 NavilShah  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 50
  • Joined: 02-November 10

Re: Searching through a datagrid

Posted 13 December 2010 - 01:53 AM

can you send me the entire project if possible.

mail me to navilshah@yahoo.in

Thanks.

This post has been edited by NavilShah: 13 December 2010 - 01:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

Re: Searching through a datagrid

Posted 13 December 2010 - 02:23 AM

sent!
Was This Post Helpful? 0
  • +
  • -

#9 NavilShah  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 50
  • Joined: 02-November 10

Re: Searching through a datagrid

Posted 13 December 2010 - 03:24 AM

According to database field naming rules there should me no spaces between field names....

But if mistake is already made use the following code...

	Dim cmdString As String = "SELECT * FROM Employee WHERE [Last Name] ='"& SearchArgument &"'"




Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1