Solved

find  datarow matching criteria

Posted on 2006-06-26
10
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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
 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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