Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table doesn't have a primary key

Status
Not open for further replies.

kettch

Programmer
Mar 5, 2001
110
US
I have a dataset that I first do a FillSchema(dataset, SchemaType.Mapped, "Table") on, and yet it still throws System.Data.MissingPrimaryKeyException when I do a dataset.tables("Table").Rows.Find(key).

The column is marked as a primary key in the table and the sql statement is just "SELECT * FROM tableName"

Anybody have any idea why this can happen?

-Thanks
 
Try setting the .PrimaryKey property of your DataTable. The property will be a DataColumn array.
 
1. If the source table has a primary key then the DataTable will have already that primary key.
If the source table has no primary key but you can identify one then you can set that primary key in
the loaded Datatable in order to apply Find.
As RiverGuy said , maybe the way you build the key is not Okay.
Here is an example with DataTable or DataSet. The source table has a primary key of two columns, let say:
name and salary (in this order).
try
{
DataTable dt = new DataTable();
//..
myDataAdapter = new SqlDataAdapter();
myDataAdapter.FillSchema (dt,SchemaType.Source);
myDataAdapter.Fill(dt);
object [] oKey = new object[2]; // for one column you should have 1 element
oKey[0]="Smith";
oKey[1]= 19500;
DataRow drFound = dt.Rows.Find(oKey);
if (drFound != null)
{
// row found
}
else
{
// that key is not in the DataTable
}


}
catch (Exception ex)
{
string sErr = ex.GetType() + ex.Message;
}
2. If there is no primary key in the source table then you can use Select in order to query a record or a set of records from the DataTable object.

-obislavu-
 
Thanks, everybody! I did end up just manually assigning a primary key. It wasn't quit the solution I was looking for, as I've never seen the PK fail to map before. But this isn't my code, and I haven't gotten a chance to read it all yet.

-Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top