table.find

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

Marc Pelletier

Hello,

This compiles but doesn't work. The primary key for MyTable is an integer.

int key = 526;
MyDataSet.MyDataRow row = (MyDataSet.MyDataRow)
MyDataSetInstance.MyTable.Rows.Find( key );

From the documentation it seems I am expected to pass an object of some
sort in but I can't figure out what.

Thanks

Marc Pelletier
 
Marc Pelletier said:
This compiles but doesn't work. The primary key for MyTable is an integer.

int key = 526;
MyDataSet.MyDataRow row = (MyDataSet.MyDataRow)
MyDataSetInstance.MyTable.Rows.Find( key );

From the documentation it seems I am expected to pass an object of some
sort in but I can't figure out what.

Does your table *know* what the primary key is? (Are you fetching the
schema with keys?)

By "doesn't work" do you mean it doesn't find the row?

Can you produce a short but complete example program which demonstrates
the problem?
 
Does your table *know* what the primary key is? (Are you fetching the
schema with keys?)

No, apparently I'm not. I am getting a "table does not have a primary
key" exception. This is a simplified version of the schema I used to
autogenerate the class:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="TideStations" elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-
microsoft-com:xml-msdata">
<xs:element name="Stations">
<xs:complexType>
<xs:sequence>
<xs:element name="Station_ID" msdata:ReadOnly="true"
msdata:AutoIncrement="true" type="xs:int" />
<xs:element name="Description" type="xs:string" />
.. many rows removed here..
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Station_ID is the primary key, but I think I will have to make that
explicit somewhere in here, won't I? I've been trying to suss out the
syntax for that but so far no luck...
By "doesn't work" do you mean it doesn't find the row?

No it throws an exception, described above.
Can you produce a short but complete example program which demonstrates
the problem?

No, I don't think I'm up to that, actually. What I've got, in a nutshell,
is this. I have a Datamanager singleton class (thanks to you), which
returns an instance of my typed dataset like this:

DataManager dm = DataManager.Instance;
TideStations st = dm.TideStationsDS;
//which is just a property whose getter establishes
//the connection and fills the dataset.

This is working okay, because I have used dataviews to get at what I want
from this, but now I want to retrieve a record directly knowing the
primary key value. So, for testing purposes, I am trying to retrieve the
record with the primary key of 526. I use this code:
int key = 526;
TideStations.StationsRow row = (TideStations.StationsRow)
st.Stations.Rows.Find( key );

But as I mentioned above ( and should have mentioned in my first post )
it is cacking because the primary key hasn't been defined in the typed
dataset.

So my real question should be
How do I define my primary key in an xsd so that xsd.exe will generate
the typed dataset using it?

Thanks

Marc
 
So my real question should be
How do I define my primary key in an xsd so that xsd.exe will generate
the typed dataset using it?

Never mind, I figured it out myself.

I didn't manage to change the typed dataset so I don't have to do it
manually each time, though. Is that possible?

This is what I did:

DataColumn[] keys = new DataColumn[1];
keys[0] = st.Tables["Stations"].Columns["Station_ID"];
st.Stations.PrimaryKey = keys;
int keyValue = 526;
TideStations.StationsRow row = (TideStations.StationsRow)
st.Stations.Rows.Find( keyValue );

And it works. I do manage to get this stuff myself sometimes :-)

Thanks

Marc
 
Hi Marc,

If you'd set the primary key within schema, the strong typed dataset would
have Stations.FindByStation_Id method for you.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Marc Pelletier said:
207.46.248.16:

So my real question should be
How do I define my primary key in an xsd so that xsd.exe will generate
the typed dataset using it?

Never mind, I figured it out myself.

I didn't manage to change the typed dataset so I don't have to do it
manually each time, though. Is that possible?

This is what I did:

DataColumn[] keys = new DataColumn[1];
keys[0] = st.Tables["Stations"].Columns["Station_ID"];
st.Stations.PrimaryKey = keys;
int keyValue = 526;
TideStations.StationsRow row = (TideStations.StationsRow)
st.Stations.Rows.Find( keyValue );

And it works. I do manage to get this stuff myself sometimes :-)

Thanks

Marc
 
Back
Top