This is a migrated thread and some comments may be shown as answers.

Grid errors on heirarchy grid insert

1 Answer 91 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 09 Jun 2014, 08:26 PM

I am getting a strange error when I try to insert records on a sub radgrid and I still have one other to build in yet.  What happens is that it grabs all the fields and inserts into the database just fine, but when it rebinds it tells me that the intPositionId from the sub table does not exist.  The following is the error that it gieves me.  This is the PK of of sub table and is inserted fine, like I said its just on rebinding of the table that it goes whacky, but if I leave page and come back normal everything I inserted is there and it works just fine.  It has something to do with the postback and rebind.  If I take out postback rebind it works just fine but then the data inserted is not there.


if (e.CommandName == RadGrid.PerformInsertCommandName && e.Item.OwnerTableView.Name == "Positions")
       {
           GridEditableItem item = e.Item as GridEditableItem;
           GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
           //int sectionId = Convert.ToInt32(item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["intSectionId"]);
           int secId = Convert.ToInt32(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["intSectionId"]);
           TextBox descrip = (TextBox)item.FindControl("txtdescription");
           TextBox scale = (TextBox)item.FindControl("txtScale");
           TextBox posType = (TextBox)item.FindControl("txtPosType");
           TextBox grade = (TextBox)item.FindControl("txtGrade");
 
           sql = "Execute usp_InsertPosition " + secId + ", '" + c.SanitizeString(descrip.Text) + "', '" + c.SanitizeString(scale.Text) + "', '" + c.SanitizeString(posType.Text) + "', '" + c.SanitizeString(grade.Text) + "'";
 
           c.InsertUpdateDelete(sql);
           myradGrid.Rebind();  """"""If I remove works fine but in sub grid gives below error.
       }


intPositionId is neither a DataColumn nor a DataRelation for table .
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: System.ArgumentException: intPositionId is neither a DataColumn nor a DataRelation for table .
 
Source Error:
 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 
 
Stack Trace:
 
 
[ArgumentException: intPositionId is neither a DataColumn nor a DataRelation for table .]
   System.Data.DataRowView.get_Item(String property) +1353926
   Telerik.Web.UI.GridTableView.PopulateDataKey(Object dataItem, DataKey key, String name) +86
   Telerik.Web.UI.GridTableView.PopulateDataKeys(Object dataItem) +194
 
[GridException: There was a problem extracting DataKeyValues from the DataSource. Please ensure that DataKeyNames are specified correctly and all fields specified exist in the DataSource.]














































1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 10 Jun 2014, 06:57 AM
Hi Kevin,

When you bind the Grid with NeedDataSource or Declarative way, you donot have to call Rebind() on Insert. Below is a sample code snippet which works fine at my end. Please take a look.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" AutoGenerateEditColumn="true" AllowPaging="True" AllowSorting="true" OnDetailTableDataBind="RadGrid1_DetailTableDataBind" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand">
    <MasterTableView DataKeyNames="CountryID" HierarchyLoadMode="ServerBind" Name="Country" CommandItemDisplay="Top" >
        <DetailTables>
            <telerik:GridTableView DataKeyNames="StateID" Name="State" HierarchyLoadMode="ServerBind" ClientDataKeyNames="StateID" CommandItemDisplay="Top">
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="StateID" Name="District" CommandItemDisplay="Top" ClientDataKeyNames="DistrictID">
                        <Columns>
                            <telerik:GridBoundColumn HeaderText="DistrictID" DataField="DistrictID" UniqueName="DistrictID">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="DistrictName" DataField="DistrictName" UniqueName="DistrictName">
                            </telerik:GridBoundColumn>
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
                <Columns>
                    <telerik:GridBoundColumn HeaderText="StateID" DataField="StateID" UniqueName="StateID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="StateName" DataField="StateName" UniqueName="StateName">
                    </telerik:GridBoundColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
        <Columns>
            <telerik:GridBoundColumn SortExpression="CountryID" HeaderText="CountryID" DataField="CountryID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn SortExpression="CountryName" HeaderText="CountryName" DataField="CountryName">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    if (!e.IsFromDetailTable)
    {
        RadGrid1.DataSource = GetDataTable("Select * from Country");
    }
}
protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
{
    GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
    switch (e.DetailTableView.Name)
    {
        case "State":
            {
                string CountryID = dataItem.GetDataKeyValue("CountryID").ToString();
                e.DetailTableView.DataSource = GetDataTable("SELECT * FROM State WHERE CountryID = '" + CountryID + "'");
                break;
            }
        case "District":
            {
                string StateID = dataItem.GetDataKeyValue("StateID").ToString();
                e.DetailTableView.DataSource = GetDataTable("SELECT * FROM District WHERE StateID = '" + StateID + "'");
                break;
            }
    }
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.PerformInsertCommandName)
    {
        GridEditableItem editItem = e.Item as GridEditableItem;
        GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
        if (e.Item.OwnerTableView.Name == "State")
        {
            int CountryID = Convert.ToInt32(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["CountryID"]);
            TextBox txtStateID = (TextBox)editItem["StateID"].Controls[0];
            TextBox txtStateName = (TextBox)editItem["StateName"].Controls[0];
 
            string insertQuery = "INSERT into  State(StateID,StateName,CountryID) values('" + txtStateID.Text + "','" + txtStateName.Text + "','" + CountryID + "')";
            InserttoGrid(insertQuery);
        }
        else if (e.Item.OwnerTableView.Name == "District")
        {
 
            int StateID = Convert.ToInt32(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["StateID"]);
            TextBox txtDistrictID = (TextBox)editItem["DistrictID"].Controls[0];
            TextBox txtDistrictName = (TextBox)editItem["DistrictName"].Controls[0];
 
            string insertQuery = "INSERT into  District(DistrictID,DistrictName,StateID) values('" + txtDistrictID.Text + "','" + txtDistrictName.Text + "','" + StateID + "')";
            SqlCommand.CommandText = insertQuery;
            InserttoGrid(insertQuery);
        }
    }
}
public void InserttoGrid(string insertQuery)
{
    try
    {
        SqlConnection.Open();
        SqlCommand.CommandText = insertQuery;
        SqlCommand.Connection = SqlConnection;
        SqlCommand.ExecuteNonQuery();
        SqlConnection.Close();
    }
    catch (Exception ex)
    {
        RadGrid1.Controls.Add(new LiteralControl("Unable to insert. Reason: " + ex.Message));
    }
}
public DataTable GetDataTable(string query)
{
    SqlDataAdapter.SelectCommand = new SqlCommand(query, SqlConnection);
    DataTable myDataTable = new DataTable();
    SqlConnection.Open();
    try
    {
        SqlDataAdapter.Fill(myDataTable);
    }
    finally
    {
        SqlConnection.Close();
    }
    return myDataTable;
}

Thanks,
Princy
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or