Grid using Store on ObjectDataSource conflict primary key on save and cannot delete ....

  1. #1

    Grid using Store on ObjectDataSource conflict primary key on save and cannot delete ....

    Greeting, I need some help ...

    This is my code

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SetTahun.aspx.cs" Inherits="WebApplication1.Setup.SetTahun1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    
        <script>
            function insertRecord(grid) {
                var store = grid.store,
                    row = store.indexOf(store.insert(0, {  })[0]);
    
                Ext.defer(function () {
                    grid.editingPlugin.startEditByPosition({ row: row, column: 0 });
                }, 100);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <ext:ResourceManager ID="ResourceManager1" runat="server" Theme="Classic">
            </ext:ResourceManager>
    
            <asp:ObjectDataSource   
                ID="TahunDS" 
                runat="server" 
                DataObjectTypeName="Belant.Asset.DAL.Tahun"
                DeleteMethod="Delete" 
                InsertMethod="Save" 
                SelectMethod="GetList" 
                TypeName="Belant.Asset.BLL.ThnManager"
                UpdateMethod="Edit">
            </asp:ObjectDataSource>
    
    
            <ext:Viewport ID="Viewport1" runat="server" Layout="FitLayout">
                <Items>
                    <ext:Panel runat="server" ID="PanelGrid" Region="North" Layout="FitLayout" Border="false">
                        <Items>
                            <ext:GridPanel ID="GridPanelTahun" runat="server" Scroll="Both" Border="false" >
    
                                <Store>
                                    <ext:Store ID="StoreTahun" 
                                        runat="server" 
                                        DataSourceID="TahunDS" 
                                        OnRefreshData="StoreTahun_RefreshData"
                                        OnBeforeStoreChanged="StoreTahun_BeforeChanged" 
                                        OnAfterRecordDeleted="StoreTahun_AfterRecordDeleted" 
                                        OnBeforeRecordInserted="StoreTahun_BeforeRecordInserted"
                                        OnAfterRecordInserted="StoreTahun_AfterRecordInserted" 
                                        OnAfterRecordUpdated="StoreTahun_AfterRecordUpdated"
                                        OnReadData="StoreTahun_ReadData"
                                        RefreshAfterSaving="Always">
                                        <Model>
                                            <ext:Model runat="server">
                                                <Fields>
                                                    <ext:ModelField Name="IDTAHUN" Type="String" />
                                                    <ext:ModelField Name="TAHUN"  Type="String" />
                                                </Fields>
                                            </ext:Model>
                                        </Model>
    
                                        <ServerProxy>
                                            <ext:PageProxy>
                                                <RequestConfig>
                                                    <EventMask ShowMask="true" CustomTarget="App.GridPanelTahun.getView().el" />
                                                </RequestConfig>
                                            </ext:PageProxy>
                                        </ServerProxy>
    
                                    </ext:Store>
                                </Store>
    
                                <ColumnModel ID="PrpColMdl" IDMode="Legacy" Enabled="true">
                                    <Columns>
                                        <ext:Column runat="server" ColumnID="IDTAHUN" DataIndex="IDTAHUN" Header="<b>ID Tahun</b>"  Sortable="True" Flex="2" > <%--DataIndex="IDTAHUN"--%>
                                            <Editor>
                                                <ext:TextField ID="e_IDTahun" runat="server" AllowBlank="false" MaxLengthText="2" MaxLength="2" />
                                            </Editor>
                                        </ext:Column>
                                        <ext:Column runat="server" ColumnID="TAHUN" DataIndex="TAHUN" Header="<b>Tahun</b>"  Sortable="True" Flex="1"> <%--DataIndex="TAHUN"--%>
                                            <Editor>
                                                <ext:TextField ID="e_Tahun" runat="server" AllowBlank="false"  />
                                            </Editor>
                                        </ext:Column>
                                    </Columns>
                                </ColumnModel>
                                
                                <BottomBar>
                                    <ext:PagingToolBar ID="PagingToolBar1" runat="server" StoreID="StoreTahun" DisplayInfo="True" />
                                </BottomBar>
    
                                <SelectionModel>
                                    <ext:RowSelectionModel runat="server" Mode="Multi">
                                        <Listeners>
                                            <Select Handler="#{btnDelete}.enable();" />
                                            <Deselect Handler="if (!#{GridPanelTahun}.selModel.hasSelection()) {
                                                                   #{btnDelete}.disable();
                                                               }" />
                                        </Listeners>
                                    </ext:RowSelectionModel>
                                </SelectionModel>
                                
                                <Plugins>
                                    <ext:CellEditing runat="server" />
                                </Plugins>
                            </ext:GridPanel>
                        </Items>
                        <Buttons>
                            <ext:Button ID="btnInsert" runat="server" Text="Tambah" Icon="Add"> <%--button --Insert-- --%>
                                <Listeners>
                                    <%--<Click Handler="#{GridPanelTahun}.insertRecord(0, 0);
                                                    #{GridPanelTahun}.getView().focusRow(0);
                                                    #{GridPanelTahun}.startEditing(0, 0);" />--%>
                                    <Click Handler="#{StoreTahun}.insert(0, {});#{GridPanelTahun}.editingPlugin.startEditByPosition({row:0, column:0});" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="btnDelete" runat="server" Text="Hapus" Icon="Delete"> <%--button --Delete-- --%>
                                <Listeners>
                                    <Click Handler="Ext.Msg.show({title:'Konfirmasi',
                                                    msg: 'Hapus data yang dipilih?',
                                                    buttons: Ext.Msg.YESNO,
                                                    fn: function(response) {
                                                        if ('yes' !== response) {
                                                            return;}
                                                        #{GridPanelTahun}.deleteSelected();
                                                        #{GridPanelTahun}.save();},
                                                    icon: Ext.MessageBox.QUESTION});" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="btnSave" runat="server" Text="Simpan" Icon="Disk"> <%--button --button Save-- --%>
                                <Listeners>
                                    <Click Handler="#{StoreTahun}.sync();" /><%--<Click Handler="#{GridPanelTahun}.save();" />--%>
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="btnRefresh" runat="server"  AutoPostBack="false" Text="Refresh" Icon="ArrowRefresh"> <%--button --Refresh-- --%>
                                <Listeners>
                                    <Click Handler="#{StoreTahun}.reload();" /> <%--<Click Handler="#{GridPanelTahun}.reload();" />--%>
                                </Listeners>
                            </ext:Button>
                        </Buttons>
                    </ext:Panel>
                </Items>
            </ext:Viewport>
    
        </form>
    </body>
    </html>
    after I insert new data then click on 'Simpan' button always show this picture :
    Click image for larger version. 

Name:	aset_1.png 
Views:	2 
Size:	26.5 KB 
ID:	25341

    and when I delete data .... they are delete from grid, but after I refresh it show up again and when I check on database ... they're not deleted
    what's solution for that error .... thank's
    Last edited by fabricio.murta; May 22, 2020 at 1:23 PM. Reason: Wrap code in [code][/code] tags.
  2. #2
    Hello @muhammadantoniussony, and welcome to Ext.NET forums!

    I have edited your message to wrap the code block in [code][/code] tags.

    I see your code sample is not something I can just copypaste and run on my side for various missing resources. I will try to help you pointing examples in Examples Explorer using this concept so you can check whether they cover your use case. Here are the examples:

    - GridPanel > DataSource_Controls > ObjectDataSource
    - GridPanel > Paging_and_Sorting > DirectMethod
    - GridPanel > Paging_and_Sorting > Page

    If that doesn't help, in your code sample, can you stub out the object data source to some mock/static data that reproduces the issue, so that we can reproduce it our side and tell you what you can do to fix the issue? You may just edit your first post to add the proper code so that we can check it.

    If you edit your thread above, also make another post here telling you did so we can check (or else we won't get notified of updates to this thread).

    You can check the examples above to a "shape" of simplified code (code behind+page on the same file, etc), and please also review our guidelines as they contain helpful information:

    - Tips for creating simplified code samples
    - More Information Required
    - Forum Guidelines

    Hope this helps!
  3. #3

    Thank you ...

    Thank you ....
    I've already repair my code on this ID=btnInsert and ID=btnDelete

                            <ext:Button ID="btnInsert" runat="server" Text="Tambah" Icon="Add"> <%--button --Insert-- --%>
                                <Listeners>
                                    <Click Handler="insertRecord(#{GridPanelTahun});" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="btnDelete" runat="server" Text="Hapus" Icon="Delete" Disabled="true"> <%--button --Delete-- --%>
                                <Listeners>
                                    <Click Handler="Ext.Msg.show({title:'Konfirmasi',
                                                    msg: 'Hapus data yang dipilih?',
                                                    buttons: Ext.Msg.YESNO,
                                                    fn: function(response) {
    				                                    if ('yes' !== response) {
    					                                    return;} else {
                                                        #{GridPanelTahun}.deleteSelected();#{StoreTahun}.sync();
                                                        #{btnDelete}.disable();} },
                                                    icon: Ext.MessageBox.QUESTION});" />
                                </Listeners>
                            </ext:Button>
    and for the code behind become :
    protected void StoreTahun_AfterRecordInserted(object sender, AfterRecordInsertedEventArgs e)
    {
        Belant.Asset.DAL.Tahun tah = e.Object<Belant.Asset.DAL.Tahun>();
        e.Keys.Add("IDTAHUN", tah.IDTAHUN.Trim());
    }
    
    protected void StoreTahun_BeforeRecordInserted(object sender, BeforeRecordInsertedEventArgs e)
    {
        //UserLogin agama = e.Object<UserLogin>();
        //am.AddItem(agama);
        Tahun tah = e.Object<Tahun>();
        Tahun ul = new Tahun();
        ThnManager tahm = new ThnManager();
        ul = tahm.GetItembyID(tah.IDTAHUN);
        if (ul != null) {
            e.Cancel = true;
            this.StoreTahun.Reload();
            this.ResourceManager1.AddScript("Ext.Msg.show({title:'Perhatian',msg: 'Field <b>ID Tahun</b> Sudah Ada <br>Data Gagal Disimpan...!',buttons: Ext.Msg.OK,icon: Ext.MessageBox.WARNING});");
        }
        else
        {
            //thn.Save(tah);
            this.StoreTahun.DataBind();
            this.StoreTahun.Reload();
            this.ResourceManager1.AddScript("Ext.Msg.show({title:'Informasi',msg: 'Data Berhasil <b>Disimpan</b> dan <b>Di-Update</b>',buttons: Ext.Msg.OK,icon: Ext.MessageBox.INFORMATION});");
        }
    }
    and now it's work fine on insert, save, delete and refresh data on grid panel using objectdatasource and store ....
    Thank's ...
  4. #4
    Hello, @muhammadantoniussony!

    Glad you could find a solution for the issue you faced, and thanks for sharing it. We really appreciate it!
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. [CLOSED] Insert, Update, Delete in CalendarPanel using ObjectDataSource
    By asiaesolutions in forum 2.x Legacy Premium Help
    Replies: 3
    Last Post: Feb 21, 2013, 11:53 AM
  2. Replies: 17
    Last Post: Sep 27, 2012, 3:18 PM
  3. [CLOSED] Store and 3 Primary Key
    By pdcase in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Nov 23, 2010, 11:21 AM
  4. [CLOSED] Gridview Primary Key
    By GmServizi in forum 1.x Legacy Premium Help
    Replies: 7
    Last Post: Aug 08, 2009, 10:08 AM
  5. Replies: 4
    Last Post: Oct 24, 2008, 12:31 PM

Posting Permissions