Hi,
I have a dropdown, which will select a table from the list and the Webdatagrid will display the values of that table in the grid. However, if I change the table from the dropdown again, it throws error as "DataKeyField is invalid". But I can see the valid DataKeyFields for the new table that I have selected. Please help me in this?
My aspx code:
protected void Page_Load(object sender, EventArgs e) { string table = DropDownList1.SelectedValue; if (table != "") { SqlDataSource1.SelectCommand = "select * from [" + DropDownList1.SelectedValue + "]"; WebDataGrid1.Visible = true; string connectionstring = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectionstring)) { conn.Open(); string sql = ""; #region *** Get the primary keys *** ArrayList alKeys = new ArrayList(); sql = String.Format("select c.COLUMN_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk, INFORMATION_SCHEMA.KEY_COLUMN_USAGE c where CONSTRAINT_TYPE = 'PRIMARY KEY' and c.TABLE_NAME = pk.TABLE_NAME and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME and pk.TABLE_NAME = '{0}'", table); SqlCommand cmd = new SqlCommand(sql, conn); cmd.CommandType = CommandType.Text; using (SqlDataReader reader = cmd.ExecuteReader()) { string keys = ""; while (reader.Read()) { alKeys.Add(reader.GetString(0)); keys += reader.GetString(0) + ", "; } keys = keys.Substring(0, keys.Length - 2); //set the grid's primary keys WebDataGrid1.DataKeyFields = keys; } #endregion } } else { SqlDataSource1.SelectCommand = "SELECT 1"; WebDataGrid1.Visible = false; } }
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { RefreshGrid(); }
private void RefreshGrid() { WebDataGrid1.DataBind(); WebDataGrid1.ScrollLeft = 0; WebDataGrid1.ScrollTop = 0; }
Stack Trace:
[InvalidOperationException: DataKeyField is invalid] Infragistics.Web.UI.Framework.Data.DataSourceAdapter.Select(DataSourceSelectArguments arguments) +635 Infragistics.Web.UI.Framework.Data.DataSourceObjectView.ExecuteSelect(DataSourceSelectArguments arguments) +73 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +19 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73 Infragistics.Web.UI.GridControls.WebDataGrid.DataBind() +909 MyWeb.Default1.RefreshGrid() in E:\Project\ASP.NET\Practice\MyWeb\MyWeb\Default1.aspx.cs:229 MyWeb.Default1.DropDownList1_SelectedIndexChanged(Object sender, EventArgs e) in E:\Project\ASP.NET\Practice\MyWeb\MyWeb\Default1.aspx.cs:299 System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +111 System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent() +134 System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +10 System.Web.UI.Page.RaiseChangedEvents() +165 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1485
Hi koushikn,
It has been some time since your post, however in case you still need assistance I would be glad to help.
When altering the displayed data in the grid in the manner you have described you should clear and rebind the datasource on each postback. You may also want to try disabling the viewstate on the grid in order to check if that helps.
Please let me know if you have any questions.
Best Regards,
Petar IvanovDeveloper Support EngineerInfragistics, Inc.http://es.infragistics.com/support