?
Solved

Find record using dataset vb.net

Posted on 2009-02-10
23
Medium Priority
?
3,491 Views
Last Modified: 2013-12-25
Hi!

Include source code for seartch for records in a dataset.

My dataset is sorted By Firmanavn, and have a listbox with
all records in dataset.

And my dataadapter is olso sorted by Firmanavn.

But DV.Sort=("FirmaID") - Sort data by FirmaID and NOT Firmanavn.

I want to search for FirmaID (Text.box)....

How can i fix this...

Please help ASAP.

Thanks
Dim DV As DataView
        Dim I As Integer
        DV = New DataView(objLeverandorerDataSet.Tables("qryRSLeverandorer"))
        DV.Sort = ("FirmaID")
        I = DV.Find(txtSearch_txt.Text)
        If I > 0 Then
            Me.BindingContext(objLeverandorerDataSet, "qryRSLeverandorer").Position = I
        Else
            MsgBox("Ingen Leverandør finnes med ID nummer : " & txtSearch_txt.Text, MsgBoxStyle.Information)
        End If

Open in new window

0
Comment
Question by:team2005
  • 12
  • 11
23 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 23598947
DataRows[] drFiltered = objLeverandorerDataSet.Tables("qryRSLeverandorer").Select("FirmaID = '" + textbox1.Text + "'");
if drFiltered.Length != 0 then you know you have the records filtered
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23598952
0
 
LVL 2

Author Comment

by:team2005
ID: 23599236
Hi!

Can you please show where to put your source code, in my code.
Thanks...

Tor
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 53

Expert Comment

by:Dhaest
ID: 23599269
Dim DV As DataView
        Dim I As Integer
        DV = New DataView(objLeverandorerDataSet.Tables("qryRSLeverandorer"))
        DV.Sort = ("FirmaID")
       DV.RowFilter = "FirmaId = "' & txtSearch_txt.Text "'"

        If DV.Count > 0 Then
            ...
        Else
            MsgBox("Ingen Leverandør finnes med ID nummer : " & txtSearch_txt.Text, MsgBoxStyle.Information)
        End If
0
 
LVL 2

Author Comment

by:team2005
ID: 23599379
Hi!

Your code dosent work, changed the code to (Attached code):
But it dosent find any records...?



Dim DV As DataView
        Dim I As Integer
        DV = New DataView(objLeverandorerDataSet.Tables("qryRSLeverandorer"))
        DV.Sort = ("LeverandorID")
        DV.RowFilter = "LeverandorID = '" & txtSearch_txt.Text & "'"
 
        I = DV.Find(txtSearch_txt.Text)
        
        If I > 0 Then
            Me.BindingContext(objLeverandorerDataSet, "qryRSLeverandorer").Position = I
        Else
            MsgBox("Ingen Leverandør finnes med ID nummer : " & txtSearch_txt.Text, MsgBoxStyle.Information)
        End If

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23599390
why do you need  I = DV.Find(txtSearch_txt.Text) ???

Can you check the following:
Dim I As Integer
        DV = New DataView(objLeverandorerDataSet.Tables("qryRSLeverandorer"))
        DV.Sort = ("LeverandorID")
        DV.RowFilter = "LeverandorID = '" & txtSearch_txt.Text & "'"

Msgbox(dv.Count)
0
 
LVL 2

Author Comment

by:team2005
ID: 23599421
Hi!

It's returning 1

Need the Code - > I = DV.Find(txtSearch_txt.Text)
To get the recordnumber for dataset to show on screen.

How do i get the record returnd, to show on screen. ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23599470
what ID do you need ? Is it a field in the database ?

Dim DV As DataView
        Dim I As Integer
        DV = New DataView(objLeverandorerDataSet.Tables("qryRSLeverandorer"))
        DV.Sort = ("LeverandorID")
        DV.RowFilter = "LeverandorID = '" & txtSearch_txt.Text & "'"

      For each dr as DataRowView in dv
           i = dr("ID")
      next
0
 
LVL 2

Author Comment

by:team2005
ID: 23599566
Hi!

Have a dataset on my form.
That shows records from qryRSLeverandorer table.

The dataset is sorted bye Firmanavn (String)

Have a textbox, that i am using for search for LeverandorID in table qryRSLeverandorer

Then i want to display this found record on screen.

Hope you anderstand what i meen.

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 23599594
In my mind, it's never a good idea to work with a generated order to retrieve the correct record. You should consider using an unique ID which will always be correct for the record, no mather how it's sorted on the screen....

If you really want to use something like your find, you 'll need to loop all your records because If I remember correctly, the find will only work on the key of the table....
Dim DV As DataView
        Dim I As Integer
        DV = New DataView(objLeverandorerDataSet.Tables("qryRSLeverandorer"))
        DV.Sort = ("LeverandorID")
        DV.RowFilter = "LeverandorID = '" & txtSearch_txt.Text & "'"
        Dim I as integer  = 0
        Dim counter as integer = 0
        For Each drv As DataRowView in dv
           If CType(drv("LeverandorID"), Integer) = Ctype(txtSearch_txt.Text , Integer) then 
              I = counter
           End if
           counter = counter + 1
        Next
        
        If I > 0 Then
            Me.BindingContext(objLeverandorerDataSet, "qryRSLeverandorer").Position = I
        Else
            MsgBox("Ingen Leverandør finnes med ID nummer : " & txtSearch_txt.Text, MsgBoxStyle.Information)
        End If

Open in new window

0
 
LVL 2

Author Comment

by:team2005
ID: 23599647
hI!

Now, its not find any record ?

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23599707
what type of column is LeverandorID  ?


(oops, we also need to remove the dv.rowfilter)
Dim DV As DataView
        Dim I As Integer
        DV = New DataView(objLeverandorerDataSet.Tables("qryRSLeverandorer"))
        DV.Sort = ("LeverandorID")
 
        Dim I as integer  = 0
        Dim counter as integer = 0
        For Each drv As DataRowView in dv
           If Ctype(drv("LeverandorID"), String) = txtSearch_txt.Text then 
              I = counter
           End if
           counter = counter + 1
        Next
        
        If I > 0 Then
            Me.BindingContext(objLeverandorerDataSet, "qryRSLeverandorer").Position = I
        Else
            MsgBox("Ingen Leverandør finnes med ID nummer : " & txtSearch_txt.Text, MsgBoxStyle.Information)
        End If

Open in new window

0
 
LVL 2

Author Comment

by:team2005
ID: 23599782
hI!

LeverandorID is the primery key.

What it must return, is the Position

Example:

Have a record in qryRSLeverandorer that are at position = 10 in the dataset
But the source, now return 83 ???


0
 
LVL 2

Author Comment

by:team2005
ID: 23599800
Hi !


Hmmmm, have sorted the dataset to "Firmanavn"
So maby its a sort problem ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23599811
Example:

Have a record in qryRSLeverandorer that are at position = 10 in the dataset
But the source, now return 83 ???

--> That's the ristk I already mentioned :)

In my mind, it's never a good idea to work with a generated order to retrieve the correct record. You should consider using an unique ID which will always be correct for the record, no mather how it's sorted on the screen....
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23599825
>> So maby its a sort problem ?

How are your records retrieved in the main form (where you want to show the data ?)
0
 
LVL 2

Author Comment

by:team2005
ID: 23599881
Hi!

LeverandorID = unique ID

My dataset and Listbox (Showing ALL records in dataset = Firmanavn - Sorted)
My scrren shows the data for record, selected from listbox.
All this is working....

Have this seek field (textbox), that i want to seek for a record = LeverandorID.
Example : Have a record that i know have LeverandorID = 100

Enter 100 in the textbox, and it must then show this data on screen.
Like i select this record from listbox.

Hope this explane thing a little better now :)

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23599953
I never use a bindingcontext, that's why I didn't understand it completely.

Back to the beginning.
What do you get in your original code for:
I = DV.Find(txtSearch_txt.Text)
or
I = DV.Find(cint(txtSearch_txt.Text))
Dim DV As DataView
        Dim I As Integer
        DV = New DataView(objLeverandorerDataSet.Tables("qryRSLeverandorer"))
--        DV.Sort = ("FirmaID")
        I = DV.Find(txtSearch_txt.Text)
        If I > 0 Then
            Me.BindingContext(objLeverandorerDataSet, "qryRSLeverandorer").Position = I
        Else
            MsgBox("Ingen Leverandør finnes med ID nummer : " & txtSearch_txt.Text, MsgBoxStyle.Information)
        End If

Open in new window

0
 
LVL 2

Author Comment

by:team2005
ID: 23600553
Hi!

If i putt 100 in the textbox.
I = 68

Position 68 = LeverandoID=38

What is going on ?



0
 
LVL 2

Author Comment

by:team2005
ID: 23600637
Hi!

Tryed to change  DV.Sort = ("FirmaID")
To : DV.Sort = ("Firmanavn")

So the Listbox = sort on Firmanavn
And Dataset is sort on Firmanavn
And DV.Sort is sort on Firmanavn

Then its working 100%, but i dont want to seartch for Firmanavn
but FirmaID

So the problem is how to Sort on Firmanavn and NOT firmaID

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23600676
I don't see the problem any more.

When you want to search for a position in your dataview, you must be sure that this order is the same as the bindingcontext (in your latest example you said that it's correct).
So what is the issue that is still hanging ? Don't you get the right record ?
0
 
LVL 2

Author Comment

by:team2005
ID: 23600696
Hi!

Have fixed it now, the problem was sorting.

I changed -> DV.Sort = ("LeverandorID") to -> DV.Sort = ("Firmanavn")

The included code works..

Thanks for helping me out.
Give you the points.


     Dim DV As DataView
        DV = New DataView(objLeverandorerDataSet.Tables("qryRSLeverandorer"))
        DV.Sort = ("Firmanavn")
        '   DV.RowFilter = "LeverandorID = '" & txtSearch_txt.Text & "'"
        Dim I As Integer = 0
        Dim counter As Integer = 0
        For Each drv As DataRowView In DV
            If CType(drv("LeverandorID"), Integer) = CType(txtSearch_txt.Text, Integer) Then
                I = counter
            End If
            counter = counter + 1
        Next
 
        If I > 0 Then
            Me.BindingContext(objLeverandorerDataSet, "qryRSLeverandorer").Position = I
            Me.objLeverandorerDataSet_PositionChanged()
        Else
            MsgBox("Ingen Leverandør finnes med ID nummer : " & txtSearch_txt.Text, MsgBoxStyle.Information)
        End If

Open in new window

0
 
LVL 2

Author Closing Comment

by:team2005
ID: 31544931
Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question