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

How to insert with modified primary key

1 Answer 155 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Helen
Top achievements
Rank 1
Helen asked on 11 Jun 2014, 03:31 PM
Hi,

I have a combination key made up by 3 fields (lead_id,location_id,seq_id). I need to increment seq_id in my code-behind before inserting into the table while the first 2 keys stay the same.  Similar to this post but I'm doing in-place editing:

http://www.telerik.com/forums/add-edit-with-user-defined-primary-key

I'm unsure if it's doable but I tried both in-place and GridTemplateColumn as suggested in the post, but couldn't get it to work. I received this error - "Exception has been thrown by the target of an invocation." after it leaves the InsertCommand event.  Is there anything I'm missing to get the seq_id modify and insert to table? I did set ReadOnly="False".

Here's my ascx:
<telerik:RadGrid ID="RadGrid1" runat="server" MasterTableView-EditMode="InPlace" DataSourceID="ObjectDataSource1" OnInsertCommand="RadGrid1_InsertCommand"
    AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True" >
    <MasterTableView AllowAutomaticDeletes="False" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AutoGenerateColumns="False" CommandItemDisplay="Top"
        DataKeyNames="lead_id,location_id,seq_id" DataSourceID="ObjectDataSource1">
        <NoRecordsTemplate>
            No Record Exists</NoRecordsTemplate>
        <Columns>
            <telerik:GridBoundColumn DataField="lead_id" DataType="System.Int32" FilterControlAltText="Filter lead_id column" HeaderText="lead_id" ReadOnly="True"
                SortExpression="lead_id" UniqueName="lead_id" Visible="False">
                <ColumnValidationSettings>
                    <ModelErrorMessage Text="" />
                </ColumnValidationSettings>
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="location_id" DataType="System.Int32" FilterControlAltText="Filter location_id column" HeaderText="location_id" ReadOnly="True"
                SortExpression="location_id" UniqueName="location_id" Visible="False">
                <ColumnValidationSettings>
                    <ModelErrorMessage Text="" />
                </ColumnValidationSettings>
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="seq_id" DataType="System.Int32" FilterControlAltText="Filter seq_id column" HeaderText="seq_id" ReadOnly="False"
                SortExpression="seq_id" UniqueName="seq_id">
                <ColumnValidationSettings>
                    <ModelErrorMessage Text="" />
                </ColumnValidationSettings>
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="current_provider" FilterControlAltText="Filter current_provider column" HeaderText="current_provider"
                SortExpression="current_provider" UniqueName="current_provider">
                <ColumnValidationSettings>
                    <ModelErrorMessage Text="" />
                </ColumnValidationSettings>
            </telerik:GridBoundColumn>
        </Columns>
 
    </MasterTableView>
</telerik:RadGrid>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
     SelectMethod="GetData" TypeName="SALTDetailProviderDALTableAdapters.salt_company_providerTableAdapter" UpdateMethod="Update" >
    <DeleteParameters>
        <asp:Parameter Name="Original_lead_id" Type="Int32" />
        <asp:Parameter Name="Original_location_id" Type="Int32" />
        <asp:Parameter Name="Original_seq_id" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="lead_id" Type="Int32" />
        <asp:Parameter Name="location_id" Type="Int32" />
        <asp:Parameter Name="seq_id" Type="Int32" />
        <asp:Parameter Name="current_provider" Type="String" />
        <asp:Parameter Name="contract_exp_dt" Type="DateTime" />
        <asp:Parameter Name="data_type" Type="Int16" />
        <asp:Parameter Name="ld_min" Type="Int32" />
        <asp:Parameter Name="line_cnt" Type="Int32" />
        <asp:Parameter Name="primary_yn" Type="String" />
    </InsertParameters>
    <SelectParameters>
        <asp:ControlParameter ControlID="LeadIdLabel" Name="lead_id" PropertyName="Text" Type="Int32" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="current_provider" Type="String" />
        <asp:Parameter Name="contract_exp_dt" Type="DateTime" />
        <asp:Parameter Name="data_type" Type="Int16" />
        <asp:Parameter Name="ld_min" Type="Int32" />
        <asp:Parameter Name="line_cnt" Type="Int32" />
        <asp:Parameter Name="primary_yn" Type="String" />
        <asp:Parameter Name="Original_lead_id" Type="Int32" />
        <asp:Parameter Name="Original_location_id" Type="Int32" />
        <asp:Parameter Name="Original_seq_id" Type="Int32" />
    </UpdateParameters>
</asp:ObjectDataSource>

Here's my code-behind:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    // Get last item's
    int count = RadGrid1.Items.Count;
    GridDataItem item = RadGrid1.Items[count - 1] as GridDataItem;
 
    // Get last item's provider name
    string providerName = item["current_provider"].Text;
 
    // Add 1 to last item's seq id
    string strSeqid = item["seq_id"].Text;
    int seqid = int.Parse(item["seq_id"].Text) + 1;
 
            
    // item about to get inserted
    GridDataInsertItem insertedItem = (GridDataInsertItem)e.Item;
     
    // Get provider name of the item about to get inserted
    TextBox providerNameTextBox = insertedItem["current_provider"].Controls[0] as TextBox;
    string providerNameInserted = providerNameTextBox.Text;
 
    // Modify the seq id
    (insertedItem["seq_id"].Controls[0] as TextBox).Text = seqid.ToString().Trim();
 
}

Thank you in advance for your help.

Helen







1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 16 Jun 2014, 11:36 AM
Hello Helen,

The approach that you are currently using seems correct, although you have Int32 type set for the seq_id field, but you are casting to a TextBox, so the underlying type of the field is of String type (if the casting actually works correctly).

Regardless the above, the error is most likely within the ObjectDataSource Insert method, so I suggest that you handle any error messages within that event and see where the root of the problem is. Following are some forum thread focusing on the same issue:
Hope that helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Helen
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or