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;
精彩评论