simple PrimaryKey question

  • Thread starter Thread starter Stuart Powers
  • Start date Start date
S

Stuart Powers

background:

I have a table in the database, 1 column in the table is a primarykey.
2 columns in the table do not allow nulls (one of those columns being the
column that is a primarykey)

problem:

DataColumn [] dc = myDataSet.Tables["mytable"].PrimaryKey;
MessageBox.Show(dc.Length.ToString()); // shows "0" when it should show 1

somehow im not reading that there is a primarykey in the table in the db.

also:

foreach(DataColumn dc in myDataSet.Tables["mytable"].Columns)
{
if(dc.AllowDBNull) //this is ALWAYS true, which should not the case
because 2 columns dont allow nulls.
//do something
}

I know i am reading the table correctly with the DataAdapter, as I can
display/edit rows in a dataset. because I can Update and save my changes,
the table /must/ have a PrimaryKey, otherwise the Update wouldn't work.. But
why can't I see it?
 
I think that you might need to get the schema from the database in order to
get the key definitions and such.
Try this after filling the dataset with your table:

MyDataAdapter.FillSchema(MyDataSet, SchemaType.Mapped,"MyTableName");

Hope it helps...
 
Back
Top