views:

57

answers:

1

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();'
+2  A: 

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;
Chris Persichetti
thank u so much , this is the solution.i think this is similar to when we should identify the data key name property for some controls in ASP.net