Solved

find  datarow matching criteria

Posted on 2006-06-26
10
459 Views
Last Modified: 2012-06-27
Hi, I am using dataTables and need to find records matching a specific criteria in Access.  I keep getting an error message saying that the table has no primary key. This is not the case as  there is a primary key, called materialID.  

Here is the code.
====================================

        dbCommand.Connection = dbConnection
        dbCommand.CommandType = CommandType.Text

        dbCommand.CommandText = "Select * from material"
        da.SelectCommand = dbCommand
        da.Fill(dtMaterials)
        Try
            dtMaterials.Rows.Find("Colbond")


 ' i get an error message here saying there's no primary key on the table.
            ColbondRate = Convert.ToSingle(dr("Rate"))

        Catch ex As Exception
            MsgBox("Could not find Colbond Rate")
        End Try


======================
Thanks,
Bsturge
0
Comment
Question by:bsturge
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 16982730
Hi bsturge,

The underlying table has a primary key but the datatable itself doesn't, set one with:

dtMaterials.PrimaryKey = "MyKeyColumn"

Before trying to do the find.

Tim Cottee
0
 
LVL 16

Assisted Solution

by:Swapnil Piparia
Swapnil Piparia earned 150 total points
ID: 16982758
Hi bsturge,
it throws this message because rows.find method takes only primary key value. while from your code i think you want to find colbond rate.

replace following

dtMaterials.Rows.Find("Colbond")
 ' i get an error message here saying there's no primary key on the table.
            ColbondRate = Convert.ToSingle(dr("Rate"))


with

Dim expression As String = "rate = ''Colbond'' "   'here replace rate with your particular column where you store colbond
Dim foundRows() As DataRow
foundRows = dtMaterials.Select(expression)
ColbondRate = Convert.ToSingle(foundRows(0))



Regards,
NetSwap
0
 

Author Comment

by:bsturge
ID: 16982966
NetSwap,
   
I get an error message: missing operand after colbond, after the second line.  Any ideas?
===================================
     
 expression = "materiahttp://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/help.jsp#hs4
Question and Answer tipslID = ''Colbond'' "   'here replace rate with your particular column where you store colbond
            foundRows = dtMaterials.Select(expression)
            ColbondRate = Convert.ToSingle(foundRows(0))


Bsturge
0
 

Author Comment

by:bsturge
ID: 16982978

TomCottee,

I added the line as suggested

   dtMaterials.PrimaryKey = "materialId"

I get the following message:

 Value of type 'String' cannot be converted to '1-dimensional array of System.Data.DataColumn'.

Regards,
Bsturge
0
 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 16983012
can u repaste your code and error again as i couldn't get your comment properly
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:bsturge
ID: 16983258
I have tried two suggestions:


TomCottee solution:

        dtMaterials.PrimaryKey = "MaterialID"

Error: Value of type 'String' cannot be converted to '1-dimensional array of System.Data.DataColumn'.  
======================================================

NetSwap Solution:

dim foundrows as datarow

            expression = "materialID = 'Colbond' "
            foundRows = dtMaterials.Select(expression)                  ' this gives foundrows{length = 1}
            ColbondRate = Convert.ToSingle(foundRows(0))            
                                                                   
Error:   ' specified  cast is not valid                            

what does the 0 in foundRows(0) refer to? Is it the first coloumn?


Regards,
Bsturge
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 150 total points
ID: 16985517
dtMaterials.PrimaryKey = New DataColumn(){dtMaterials.Columns("MaterialID")}

Bob
0
 
LVL 34

Accepted Solution

by:
Sancler earned 200 total points
ID: 16985967
>>
what does the 0 in foundRows(0) refer to? Is it the first coloumn?
<<

No it is the first (and in this case only) row in the array of found rows.  What you want is not the whole row, but the column containing "Rate" in that row, so use

            ColbondRate = Convert.ToSingle(foundRows(0)("Rate"))

unless you are going to go down the Primary Key and .Find route.        

Roger

Evening, Bob ;-)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16986063
Hellooooo, Roger dodger =:)  Where do you hail from?  Afternoon here in Ohio, USA

Bob
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16986696
Bob

Bottom left hand corner of Wales - UK.  Now 1940 hours with us.

Roger

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now