我知道有很多其他线程相似,但它们没有帮助。所以我在下面的同一行收到一些错误消息 -
ds.Tables["Customers"].PrimaryKey = new DataColoumn[]{ds.Tables["Customers"].Coloumns["ID"]};
错误消息是
整个方法如下 -
private void GetDataFromDB()
{
string CS = ConfigurationManager.ConnectionStrings["AK"].ConnectionString;
SqlConnection con = new SqlConnection(CS);
string sqlQuery = "SELECT * from tblScheduledCalls";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
DataSet ds = new DataSet();
da.Fill(ds,"Customers");
ds.Tables["Customers"].PrimaryKey = new DataColoumn[]{ds.Tables["Customers"].Coloumns["ID"]};
Cache.Insert("DATASET", ds, null, DateTime.Now.AddHours(24), System.Web.Caching.Cache.NoSlidingExpiration);
gvCustomers.DataSource = ds;
gvCustomers.DataBind();
lblMessage.Text = "Data Loaded from Database";
}
在上面的代码中,我已经在sql mgt studio中将CustomerId声明为标识主键。
这是一个如此愚蠢的错误。但现在执行时会出现另一个错误
“表没有主键” 在下面的代码中
protected void gvCustomers_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
if (Cache["DATASET"] != null)
{
DataSet ds = (DataSet)Cache["DATASET"];
DataRow dr = ds.Tables["Customers"].Rows.Find(e.Keys["ID"]);
dr["Names"] = e.NewValues["Names"];
dr["ContactNo"] = e.NewValues["ContactNo"];
dr["Email"] = e.NewValues["Email"];
dr["City"] = e.NewValues["City"];
dr["State"] = e.NewValues["State"];
Cache.Insert("DATASET", ds, null, DateTime.Now.AddHours(24), System.Web.Caching.Cache.NoSlidingExpiration);
gvCustomers.EditIndex = -1;
GetDataFromCache();
}
}
以下行错误 - DataRow dr = ds.Tables [“Customers”]。Rows.Find(e.Keys [“ID”]);
我已经在sql mgt studio中以及上面的GetDataFromDB()方法中将CustomerId作为身份主键。为什么?