简体   繁体   中英

Table doesn't have a primary key

i get the following exception (missing primary key) in the line of using Find() method

"Table doesn't have a primary key."

I've rechecked the Database and all Primary Key columns are set correctly.

my code:

DataTable dt = p.GetAllPhotos(int.Parse(Id));
DataTable temp = new DataTable();
temp = dt.Clone();
temp = (DataTable)(Session["currentImage"]);
DataTable dtvalid = new DataTable();
dtvalid = dt.Clone();
DataRow[] drr = new DataRow[1];
drr[0] = dt.Rows.Find((int.Parse(temp.Rows[0]["photoId"].ToString()))+1);
foreach (DataRow dr in drr)
{
    dtvalid.ImportRow(dr);
}
dtvalid.AcceptChanges();'

You need to set the PrimaryKey property of your DataTable object before you call Find

DataColumn[] keyColumns = new DataColumn[1];
keyColumns[0] = dt.Columns["<columnname>"];
dt.PrimaryKey = keyColumns;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM