Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching Rows

Status
Not open for further replies.

dedo8816

Programmer
Oct 25, 2006
94
0
0
GB
I've searched for the answer for this question on google, searched multiple forums and still no answers and im starting to think maybe im either asking the wrong question or going about it all wrong.

Heres my problem, i have a form, inside the form are text boxes, a navigation bar, a datagrid and a search button. The text boxes, navigation bar and datagrid are all bound to a dataset. I have 1 text box called Txsearch.text and 1 button called Search.

Basically when someone enter's an item number into Txsearch and clicks search, the fields in the form will jump to that exact record.

Up until now ive been trying to code my search button to use the rows.find method but in my obvious stupidity i cannot get it to work. Now i have downloaded the 101 samples from MSDN and found that they use:

Me.CustomersTableAdapter.FillByCompanyName(Me.NorthwindDataSet.Customers, searchToolStripTextBox.Text)

So ive tried to use this as well changing the various names to suit my form. My newest problem using this method is "FillByNo". VB.net is telling me that FillByNo is not a member of my WindowsApplication1....

When im typing this into VB.net, as im sure you are all aware, VB kind of lists all my available options depending of what im typing, ".FillBy" is not an option from that list.

Do i have to declare this somewhere on the code, how do i make this a member of my application or what am i doing wrong...

This question is really bugging the life out of me... It didn't take me this long to figure it out in standard VB6

Please help, and thanks to all that reply

D

 
To take a step back, here is what it is doing in non-techie speak.

Your CusomtersTableAdapter is something Visual Studio created for you via a wizard. It's not a .Net thing really, but more of a Visual Studio feature utilized to save you work creating your project.

The same thing applies with your FillByCompanyName method of your TableAdapter--Visual Studio created it for you. It's not built-in.

So, since Visual Studio did not create a FillByNo method for you, it's not going to work.

As I have suggested in many threads in this forum--my suggestion to a beginner is to forget about any of the wizard-generated code for data access.

I've been working on an application recently. For my data access, I am using SqlConnections, SqlCommands, SqlDataAdapters, DataSets and DataViews. A couple forms in my application display DataGridViews for data editing. To filter out my data for the DataGridView (let's say the user is searching for a specific account #), I simply bind my DataGridView to a DataView and set the .RowFilter property of the DataView.
 
Riverguy,

Thanks for the advice, i've spent the last few hours reading up about the .find method using both SQL and just the built in stuff from VB.net. SQL baffles me, VB.net baffles me. Im starting to think that moving forward would be simpler if i stayed with standard VB6 but im not one to let something like this beat me.

I have this full program working in VB6 and i cannot understand why vb.net is so different.

VB6 Code for my search button:

If DataEnvironment1.rsCommand1.EOF Then
DataEnvironment1.rsCommand1.MoveFirst
End If

DataEnvironment1.rsCommand1.MoveFirst
DataEnvironment1.rsCommand1.Find ("[No] ='" & (TxtSearch.Text) & "'")
If DataEnvironment1.rsCommand1.EOF Then
DataEnvironment1.rsCommand1.MoveFirst
End If

I have no problems anywhere else on my program, all i need is this translated to vb.net which is seeming impossible to me.

This is what ive come up with tonight without error handling:

Private Sub CmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSearch.Click
Dim mydatarow As DataRow
Dim mydatatable As DataTable
mydatatable = Label_LocationsDataSet.RTA_Locations
mydatarow = mydatatable.Rows.Find("[No] ='" & (TxSearch.Text) & "'")
DataGridView1.DataSource = mydatatable
End Sub

And althought his appears to be doing something on the form, it does not return anything, my datagridview does not move to the record im requesting.
Ive tried the below code like this as well as what is shown above...

mydatarow = mydatatable.Rows.Find(TxSearch.Text)

Any books i have tell me that the above works, what am i doing wrong?
 
Well, first of all, you're using the DataRowCollection.Find function improperly. You don't pass in a filter expression, you pass in the value of the primary key of the DataTable. So, if [No] was your primary key column, the .Find function call would look like this:
mydatarow = mydatatable.Rows.Find(TxSearch.Text)

However, you can use a filter expression with the .Select function of the DataTable. Try this code below:
Code:
    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        Dim dt As DataTable = CType(Me.DataGridView1.DataSource, DataTable)
        Dim cm As CurrencyManager = Me.BindingContext(dt)
        Dim dr As DataRow
        Try
            dr = dt.Select("[No] ='" & (TxSearch.Text) & "'")(0)
        Catch ex As Exception

        End Try
        If Not dr Is Nothing Then
            For i As Integer = 0 To dt.Rows.Count - 1
                If dt.Rows(i) Is dr Then
                    cm.Position = i
                    Exit For
                End If
            Next
        Else
            MessageBox.Show("row not found")
        End If
        dt = Nothing
        cm = Nothing
        dr = Nothing
    End Sub
 
Riverguy,

this is exactly what ive been trying to do. I still have to work out how to have the record in the navigation bar equal the same as the current grid view but i think i can work that one out myself.

Thank you for your help

D
 
Riverguy,

I'm getting an IndexOutOfRangeException

The code highlighted by vb.net is:

dr = dt.Select("[No] ='" & (TxSearch.Text) & "'")(0)

Is there error handling i can use to prevent this?

Thanks

D
 
Try this

Code:
If dt.Select("[No] ='" & (TxSearch.Text) & "'").Length > 0 Then
  dr = dt.Select("[No] ='" & (TxSearch.Text) & "'")(0)
Else
  dr = nothing
End If
 
Riverguy,

Sorry for my late reply, this code worked perfect. Thank you very much for your help!

D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top