Solved

find  datarow matching criteria

Posted on 2006-06-26
10
461 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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