PC Review


Reply
Thread Tools Rate Thread

DataRowCollection.Find Method

 
 
Marc Pelletier
Guest
Posts: n/a
 
      6th Jan 2005
Hello,

I am trying to use the find method to locate a particular record in a
table. The code is :
object[]findTheseVals = new object[2];
findTheseVals[0] = station_ID; // an integer
findTheseVals[1] = dt; // a datetime
DataRow foundRow = dsStationDays.Tables["StationDays"].Rows.Find(
findTheseVals);

It is virtually verbatim from the help file. The StationDays table has a
primary key on an integer and datetime field combination, but I get a
[MissingPrimaryKeyException: Table doesn't have a primary key.] exception
when I execute the code. I have included the sql below that creates the
table:

CREATE TABLE [dbo].[StationDays] (
[Station_ID] [int] NOT NULL ,
[Day] [smalldatetime] NOT NULL ,
[Range] [float] NULL ,
[HighTide] [float] NULL ,
[LowTide] [float] NULL ,
[HighTime] [smalldatetime] NULL ,
... many more ...
[MoonSet] [smalldatetime] NULL ,
[MoonPhase] [float] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[StationDays] WITH NOCHECK ADD
CONSTRAINT [PK_StationDays] PRIMARY KEY CLUSTERED
(
[Station_ID],
[Day]
) ON [PRIMARY]
GO

CREATE UNIQUE INDEX [StationDay] ON [dbo].[StationDays]([Station_ID],
[Day]) ON [PRIMARY]
GO

I'm not clear if I have an sql problem, or a csharp problem, but
hopefully someone here can help me.

Thanks

Marc Pelletier
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TXJOb2JvZHk=?=
Guest
Posts: n/a
 
      6th Jan 2005

You must register those primary eys with your DataTable.

for example:

myDataTable.PrimaryKey = new DataColumn[] {
myDataTable.Columns["Station_ID"], myDataTable.Columns["Day"] };
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      6th Jan 2005
Marc,

Did you ever look at the Dataview.find method, you see it seldom in these
newsgroups, however it is in my opinion very fine to use.

http://msdn.microsoft.com/library/de...sfindtopic.asp

I hope this helps?

Cor


 
Reply With Quote
 
Marc Pelletier
Guest
Posts: n/a
 
      7th Jan 2005
"=?Utf-8?B?TXJOb2JvZHk=?=" <(E-Mail Removed)> wrote in
news:A2A753E8-056F-4FD9-8E57-(E-Mail Removed):

> myDataTable.PrimaryKey = new DataColumn[] {
> myDataTable.Columns["Station_ID"], myDataTable.Columns["Day"] };
>


That works. Thank you very much.

Marc Pelletier


PS are you related to the Mr Nobody who spills all the milk and dumps the
Lego on the floor at my house?
 
Reply With Quote
 
Marc Pelletier
Guest
Posts: n/a
 
      7th Jan 2005
"Cor Ligthert" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Did you ever look at the Dataview.find method, you see it seldom in
> these newsgroups, however it is in my opinion very fine to use.
>
> http://msdn.microsoft.com/library/de...y/en-us/cpref/
> html/frlrfsystemdatadataviewclassfindtopic.asp
>


Thanks for reminding me about the DataView, I found it too painful to use
earlier, but I should revisit the topic.

cheers

Marc Pelletier
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataRowCollection.Find Scott Adams Microsoft Dot NET 2 7th Jul 2005 10:56 PM
DataRowCollection InsertAt method msnews.microsoft.com Microsoft ADO .NET 1 11th Mar 2005 01:24 AM
DataRowCollection.InsertAt method Dei Microsoft ADO .NET 1 9th Mar 2005 06:49 PM
DataRowCollection.Find Method throws invalidcastexception Sagaert Johan Microsoft Dot NET Compact Framework 1 31st Dec 2004 12:40 AM
using the InsertAt method of the DataRowCollection class =?Utf-8?B?RGVycmljaw==?= Microsoft Dot NET 1 31st Oct 2004 09:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:54 AM.