简体   繁体   中英

update and delete in jqGrid not working

I'm experiencing a bad situation with jqGrid .

I'm using asp.net 4.0 with jqGrid added to the .aspx page. Inorder to add/edit/delete the data i'm using a SqlDataSource for this purpose.

I've configured default.aspx page like this:

Links:

<link rel="stylesheet" type="text/css" media="screen" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.0/themes/redmond/jquery-ui.css" />
    <link href="Styles/ui.jqgrid.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="Scripts/i18n/grid.locale-en.js" type="text/javascript"></script>
    <script src="js/trirand/jquery.jqGrid.min.js" type="text/javascript"></script>

And in my form i've the SqlDataSource and trirand jqGrid like this:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Test_1ConnectionString %>"
        SelectCommand="SELECT * FROM [Companies]" 
        UpdateCommand="UPDATE [Companies] SET [CompanyType] = @CompanyType,[Country] = @Country,[State] = @State,[ZipCode] = @ZipCode WHERE [CompanyId] = @CompanyId"
        InsertCommand="INSERT INTO [Companies]([CompanyType],[Country],[State],[ZipCode]) VALUES (@CompanyType,@Country,@State,@ZipCode)" 
        DeleteCommand="DELETE FROM [Companies] WHERE [CompanyId] = @CompanyId">
        <DeleteParameters>
            <asp:Parameter Name="CompanyId" Type="Int32"/>
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="CompanyType" Type="String" />
            <asp:Parameter Name="Country" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="ZipCode" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="CompanyType" Type="String" />
            <asp:Parameter Name="Country" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="ZipCode" Type="String" />
            <asp:Parameter Name="CompanyId" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <trirand:JQGrid ID="jQGrid1" DataSourceID="SqlDataSource1" runat="server" AppearanceSettings-Caption="Company Details"
        AppearanceSettings-ShowCaptionGridToggleButton="true" 
        onrowdeleting="jQGrid1_RowDeleting">
        <Columns>
            <trirand:JQGridColumn DataField="CompanyId" Searchable="true" />
            <trirand:JQGridColumn DataField="CompanyType" Editable="true" />
            <trirand:JQGridColumn DataField="Country" Editable="true" />
            <trirand:JQGridColumn DataField="State" Editable="true" />
            <trirand:JQGridColumn DataField="ZipCode" Editable="true" />
        </Columns>
        <EditDialogSettings Modal="true" CloseAfterEditing="true" SubmitText="Submit Details"
            Caption="Edit Company" />

        <SearchDialogSettings Modal="true" FindButtonText="Search Company" />
        <ToolBarSettings ShowEditButton="true" ShowAddButton="true" ShowDeleteButton="true" ShowInlineAddButton="true"
            ShowSearchButton="true" ShowRefreshButton="true" />
    </trirand:JQGrid>

Everything looked fine until i've looked into the functionality of what is going on.

At first i've tried to add a row to the jqGrid , it worked !.

Then when i try to edit/delete particular row, it ended in vain. edit/update and delete functionalities doesnot work.

Even i've added the rowdeleting in codebehind to check whether i'm getting the appropriate data or not.

I've added the following function using the properties of jQGrid

protected void jQGrid1_RowDeleting(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)
        {

        }

When i place a debugger at the { the ei could see the data that is coming when a record is being selected.

When i add a row it adds it to the jqGrid and also to the Database. This works great !

Why is it not deleting from the db or even from the jqGrid ?

Is it the fault of the UpdateCommand / DeleteCommand in the SqlDataSource that i've setup ? or do i need to set up additional functionality to implement these things on behalf of jqGrid ?

Note: When i could see the data selected in the RowDeleting method in the CodeBehind file i could delete it explicitly by adding some ado.net code. Please post if there is any solution avaliable to do it with jqGrid.

Thanks for your comments/answers posted.

Edit

I found the problem when i run the Sql Profiler.

When i edit my colum with form edit, there is not id being passed to it. Here is what my sql profiler got

    exec sp_executesql N'UPDATE [Companies] SET [CompanyType] = @CompanyType,[Country] = @Country,

[State] = @State,[ZipCode] = @ZipCode WHERE [CompanyId] = @CompanyId',N'@CompanyType nvarchar

(7),@Country nvarchar(3),@State nvarchar(5),@ZipCode nvarchar(4000),@CompanyId nvarchar

(4000)',@CompanyType=N'pvt',@Country=N'abc',@State=N'def',@ZipCode=NULL,*@CompanyId=NULL*

But when i edit a column in jqGrid using Inline edit option i could see my CompanyId being not null.

    exec sp_executesql N'UPDATE [Companies] SET [CompanyType] = @CompanyType,[Country] = @Country,

[State] = @State,[ZipCode] = @ZipCode WHERE [CompanyId] = @CompanyId',N'@CompanyType nvarchar

(7),@Country nvarchar(3),@State nvarchar(5),@ZipCode nvarchar(4000),@CompanyId nvarchar

(4000)',@CompanyType=N'pvt',@Country=N'abc',@State=N'def',@ZipCode=NULL,*@CompanyId=3*

Can some one please point out why is inline query passing id column and why form editing doesnot pass the id value ?

I don't use commercial version of jqGrid/jqSuite myself, but I try guess that you should add key: true to the definition of CompanyId column. It seems that it corresponds the value PrimaryKey=true in trirand:JQGridColumn definition of column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM