How to stop deleting the records based on the condition.

  1. #1

    How to stop deleting the records based on the condition.

    Hi Folks,

    I have situation like need to delete the records based on the condition means if data exists in data base i need to tell the user saying data exists. here am using sql datasource , store and GridPanel controls

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <ext:ResourceManager ID="ResourceManager1" runat="server" Theme="Slate">
            </ext:ResourceManager>
            <table cellpadding="2" cellspacing="2" width="100%" style="font-family: verdana;
                font-size: 11px;">
                <tr>
                    <td colspan="4" align="center" style="font-size: 25px">
                        Categories
                    </td>
                </tr>
                <tr>
                    <td colspan="4" style="height: 20px" />
                </tr>
                <tr>
                    <td colspan="4" width="">
                        <ext:Label runat="server" ID="lblDesc" Html="Define categories to group contacts." />
                    </td>
                </tr>
                <tr>
                    <td colspan="4" align="justify" width="">
                        <script type='text/javascript'>
                            function MoveUpOrDown(command, store, rowIndex, colIndex, propertyToSwap) {
                                var newIndex = -1;
                                if (command == 'MoveUp' && rowIndex > 0) {
                                    newIndex = rowIndex - 1;
                                }
                                else if (command == 'MoveDown' && rowIndex < store.data.items.length - 1) {
                                    newIndex = rowIndex + 1;
                                }
    
                                if (newIndex !== -1) {
                                    var curRecord = store.getAt(rowIndex);
                                    var newRecord = store.getAt(newIndex);
                                    var oldvalue = curRecord.get(propertyToSwap);
                                    var newvalue = newRecord.get(propertyToSwap);
                                    curRecord.set(propertyToSwap, newvalue);
                                    newRecord.set(propertyToSwap, oldvalue);
                                    store.sort(propertyToSwap, "ASC");
                                    return false;
                                }
                                return true;
                            }
    
                            function IsEnabledRender(value) {
                                if (value === "")
                                    return '';
                                else {
                                    value = (value == true) ? 1 : 0;
                                    return IsEnabledcmb.getStore().data.items[value].data.text;
                                }
                            }
    
                        </script>
                        <asp:SqlDataSource ID="GridPanel1_SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:WizardConnstr%>"
                            SelectCommand="select gsCustomersTypeID,Type, Enabled,SortOrder from gsCustomersType order by SortOrder asc"
                            InsertCommand="insert into gsCustomersType(Type,dateadded,enabled,SortOrder) values(@Type,Getdate(),1,(select max(sortorder)+1 from gsCustomersType))"
                            DeleteCommand=" if((select COUNT(1) from gsCustomers where gsCustomersTypeID=@gsCustomersTypeID)>0)    RAISERROR ('Categories cannot be deleted',16,1) else delete from gsCustomersType where gsCustomersTypeID=@gsCustomersTypeID"                        UpdateCommand="update gsCustomersType set  Type=@Type,Enabled=@Enabled, SortOrder=@SortOrder where gsCustomersTypeID=@gsCustomersTypeID">
                            <DeleteParameters>
                                <asp:Parameter Name="gsCustomersTypeID" Type="Int32" />
                            </DeleteParameters>
                            <UpdateParameters>
                                <asp:Parameter Name="Type" Type="String" />
                                <asp:Parameter Name="Enabled" Type="String" />
                                <asp:Parameter Name="gsCustomersTypeID" Type="Int32" />
                            </UpdateParameters>
                            <InsertParameters>
                                <asp:Parameter Name="Type" Type="String" />
                            </InsertParameters>
                        </asp:SqlDataSource>
                        <ext:Store ID="GridPanel1_Store" runat="server" DataSourceID="GridPanel1_SqlDataSource1" OnBeforeRecordDeleted  >
                            <Reader>
                                <ext:JsonReader IDProperty="gsCustomersTypeID">
                                    <Fields>
                                        <ext:RecordField Name="Type" />
                                        <ext:RecordField Name="Enabled" />
                                        <ext:RecordField Name="SortOrder" />
                                    </Fields>
                                </ext:JsonReader>
                            </Reader>
                            <Listeners>
                                <LoadException Handler="Ext.Msg.alert(' Load failed', e.message || e);" />
                                <CommitFailed Handler="Ext.Msg.alert('Commit failed', 'Reason: ' + msg);" />
                                <SaveException Handler="Ext.Msg.alert('Save failed', e.message || e);" />
                                <CommitDone Handler="Ext.Msg.alert('Commit', 'The data successfully saved');" />
                            </Listeners>
                        </ext:Store>
                        <ext:GridPanel runat="server" ID="GridPanel1" StoreID="GridPanel1_Store" Height="230"
                            AutoExpandColumn="Type" TrackMouseOver="false">
                            <Listeners>
                                <Command Handler="if (command === 'MoveUp' || command === 'MoveDown') {  MoveUpOrDown(command, #{GridPanel1_Store}, rowIndex, colIndex, 'SortOrder');return false;} " />
                            </Listeners>
                            <ColumnModel runat="server" ID="GridPanel1_ColumnModel1">
                                <Columns>
                                    <ext:Column DataIndex="Type" Header="Name">
                                        <Editor>
                                            <ext:TextField ID="TextField1" runat="server" MaxLength="50" />
                                        </Editor>
                                    </ext:Column>
                                    <ext:Column DataIndex="Enabled" Header="Enabled">
                                        <Editor>
                                            <ext:ComboBox runat="server" ID="IsEnabledcmb" Width="50" SelectedValue="">
                                                <Items>
                                                    <ext:ListItem Text='No' Value='0' />
                                                    <ext:ListItem Text='Yes' Value='1' />
                                                </Items>
                                            </ext:ComboBox>
                                        </Editor>
                                        <Renderer Fn="IsEnabledRender" />
                                    </ext:Column>
                                    <ext:ImageCommandColumn>
                                        <Commands>
                                            <ext:ImageCommand Icon="ArrowUp" CommandName="MoveUp" />
                                            <ext:ImageCommand Icon="ArrowDown" CommandName="MoveDown" />
                                        </Commands>
                                    </ext:ImageCommandColumn>
                                </Columns>
                            </ColumnModel>
                            <SelectionModel>
                                <ext:RowSelectionModel runat="server" ID="SelectionModel1" />
                            </SelectionModel>
                            <TopBar>
                                <ext:Toolbar ID="Toolbar1" runat="server">
                                    <Items>
                                        <ext:Button runat="server" ID="btnAdd" Text="Add" Icon="Add">
                                            <Listeners>
                                                <Click Handler="#{GridPanel1}.insertRecord(0, {});#{GridPanel1}.getView().focusRow(0);#{GridPanel1}.startEditing(0, 0);" />
                                            </Listeners>
                                        </ext:Button>
                                        <ext:Button runat="server" ID="btnDelete" Text="Delete" Icon="Delete">
                                            <Listeners>
                                                <Click Handler="#{GridPanel1}.deleteSelected();" />
                                            </Listeners>
                                        </ext:Button>
                                        <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                                        <ext:Label runat="server" ID="lblInfoMsg" Html="<font color=&quot;white&quot;>Double Click to Edit</font>" />
                                    </Items>
                                </ext:Toolbar>
                            </TopBar>
                            <BottomBar>
                                <ext:Toolbar ID="Toolbar2" runat="server">
                                    <Items>
                                        <ext:Button runat="server" ID="btnSave" Text="Save" Icon="Disk">
                                            <Listeners>
                                                <Click Handler="#{GridPanel1}.save();" />
                                            </Listeners>
                                        </ext:Button>
                                        <ext:ToolbarFill ID="ToolbarFill2" runat="server" />
                                        <ext:Label runat="server" ID="lblbuttomInfoMsg" Html="<font color=&quot;white&quot;>Please Click on save button after add/updating the details</font>" />
                                    </Items>
                                </ext:Toolbar>
                            </BottomBar>
                        </ext:GridPanel>
                    </td>
                </tr>
            </table>
        </div>
        </form>
    </body>
    </html>
    in above code, In delete command am checking if records exists in table, I want to give the alert message to user "The data exists " and cancel the delete operation. with the current code user able to some techinical sql excption not user friendly message.

    Can you guys help me here how to this.
  2. #2
    Hi,

    There is no Ext.Net confirmation mechanism in a SqlDataSource case. It's confirmed automatically if there is no any SQL error.

    You should handle the SqlDataSource's Deleted event to handler errors.
    http://forums.asp.net/t/978920.aspx/1

    Also you can be interested in the Store's BeforeRecordDeleted and AfterRecordDeleted events.

Similar Threads

  1. Replies: 0
    Last Post: May 01, 2012, 9:43 AM
  2. Replies: 5
    Last Post: Feb 23, 2012, 8:00 AM
  3. Replies: 5
    Last Post: Jun 14, 2011, 11:47 AM
  4. Replies: 3
    Last Post: Jun 29, 2010, 2:54 PM
  5. GridPanel Store and Deleting Records
    By Timothy in forum 1.x Legacy Premium Help
    Replies: 8
    Last Post: Sep 07, 2008, 12:18 PM

Posting Permissions