DataRowCollection.Find Method

  • Thread starter Thread starter Marc Pelletier
  • Start date Start date
M

Marc Pelletier

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
 
You must register those primary eys with your DataTable.

for example:

myDataTable.PrimaryKey = new DataColumn[] {
myDataTable.Columns["Station_ID"], myDataTable.Columns["Day"] };
 
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?
 
Back
Top