Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

find  datarow matching criteria

Posted on 2006-06-26
10
Medium Priority
?
470 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

885 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