Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

find  datarow matching criteria

Posted on 2006-06-26
10
Medium Priority
?
468 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 600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 600 total points
ID: 16985517
dtMaterials.PrimaryKey = New DataColumn(){dtMaterials.Columns("MaterialID")}

Bob
0
 
LVL 34

Accepted Solution

by:
Sancler earned 800 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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