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

Insert not working in radgrid with sqldatasource

1 Answer 197 Views
Grid
This is a migrated thread and some comments may be shown as answers.
GlenB
Top achievements
Rank 1
GlenB asked on 17 May 2010, 04:25 AM
I have used the demo (located at http://demos.telerik.com/aspnet-ajax/toolbar/examples/applicationscenarios/gridcommanditem/defaultvb.aspx?product=grid) to create a radgrid with a command item toolbar. (I had to modify the inline code from C# to VB). Everything works fine except for the INSERT functions.

When inserting it shows the HospID and UserID fields as editable (HospID is autogenerated and UserID is a cookie parameter) which it doesn't do on the demo (The CustomerID is non-editable on demo). When I insert a Hospital name and click Add this hospital nothing happens.

The code is as follows:
    <form id="form1" runat="server"
<telerik:RadCodeBlock ID="radcodeblock1" runat="server"
    <script type="text/javascript"
        function onToolBarClientButtonClicking(sender, args) { 
            var button = args.get_item(); 
            if (button.get_commandName() == "DeleteSelected") { 
                args.set_cancel(!confirm('Delete all selected hospitals?')); 
            } 
        } 
    </script> 
</telerik:RadCodeBlock> 
    <telerik:RadScriptManager ID="ScriptManager1" runat="server"
    </telerik:RadScriptManager> 
       <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
            <AjaxSettings> 
                <telerik:AjaxSetting AjaxControlID="RadGrid1"
                    <UpdatedControls> 
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
                    </UpdatedControls> 
                </telerik:AjaxSetting> 
            </AjaxSettings> 
        </telerik:RadAjaxManager> 
         
        <telerik:RadGrid ID="RadGrid1" AllowAutomaticUpdates="true" AllowAutomaticDeletes="true" 
            DataSourceID="ds_Hospitals" Width="97%" AllowSorting="True" 
            AutoGenerateColumns="true" AllowPaging="True" GridLines="None" runat="server" 
            ShowFooter="True" AllowMultiRowSelection="True" PageSize="7" AllowMultiRowEdit="True" 
            HorizontalAlign="NotSet" OnItemDeleted="RadGrid1_ItemDeleted"
            <PagerStyle Mode="NextPrevAndNumeric" /> 
            <MasterTableView Width="100%" GridLines="None" CommandItemDisplay="Top" DataSourceID="ds_Hospitals" 
                EditMode="InPlace" HorizontalAlign="NotSet" DataKeyNames="HospID, UserID" AllowAutomaticInserts="True" AutoGenerateColumns="True"
                <CommandItemTemplate> 
                    <telerik:RadToolBar ID="RadToolBar1" OnButtonClick="RadToolBar1_ButtonClick" runat="server" OnClientButtonClicking="onToolBarClientButtonClicking"
                        <Items> 
                            <telerik:RadToolBarButton Text="Edit selected" CommandName="EditSelected" ImageUrl="~/images/toolbar/Edit.gif" Visible='<%# RadGrid1.EditIndexes.Count = 0 %>'></telerik:RadToolBarButton> 
                            <telerik:RadToolBarButton Text="Update" CommandName="UpdateEdited" ImageUrl="~/images/toolbar/Update.gif" Visible='<%# RadGrid1.EditIndexes.Count > 0 %>'></telerik:RadToolBarButton> 
                            <telerik:RadToolBarButton Text="Cancel editing" CommandName="CancelAll" ImageUrl="~/images/toolbar/Cancel.gif" Visible='<%# RadGrid1.EditIndexes.Count > 0 OrElse RadGrid1.MasterTableView.IsItemInserted %>'></telerik:RadToolBarButton> 
                            <telerik:RadToolBarButton Text="Add new" CommandName="InitInsert" ImageUrl="~/images/toolbar/AddRecord.gif" Visible='<%# Not RadGrid1.MasterTableView.IsItemInserted %>'></telerik:RadToolBarButton> 
                            <telerik:RadToolBarButton Text="Add this hospital" CommandName="PerformInsert" ImageUrl="~/images/toolbar/Insert.gif" Visible='<%# RadGrid1.MasterTableView.IsItemInserted %>'></telerik:RadToolBarButton> 
                            <telerik:RadToolBarButton Text="Delete selected hospitals" CommandName="DeleteSelected" ImageUrl="~/images/toolbar/Delete.gif"></telerik:RadToolBarButton> 
                            <telerik:RadToolBarButton Text="Refresh hospital list" CommandName="RebindGrid" ImageUrl="~/images/toolbar/Refresh.gif"></telerik:RadToolBarButton> 
                        </Items> 
                    </telerik:RadToolBar> 
                </CommandItemTemplate> 
            </MasterTableView> 
            <ClientSettings> 
                <Selecting AllowRowSelect="True" EnableDragToSelectRows="True" /> 
            </ClientSettings> 
        </telerik:RadGrid> 
 
    <asp:SqlDataSource ID="ds_Hospitals" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [Sub_Hospitals] WHERE [HospID] = @HospID" InsertCommand="INSERT INTO [Sub_Hospitals] ([UserID], [Hospitalname]) VALUES (@UserID, @Hospitalname)" 
        SelectCommand="SELECT * FROM [Sub_Hospitals] WHERE ([UserID] = @UserID) ORDER BY [Hospitalname]" 
        UpdateCommand="UPDATE [Sub_Hospitals] SET [UserID] = @UserID, [Hospitalname] = @Hospitalname WHERE [HospID] = @HospID"
        <SelectParameters> 
            <asp:CookieParameter CookieName="UserID" Name="UserID" Type="Int32" /> 
        </SelectParameters> 
        <DeleteParameters> 
            <asp:Parameter Name="HospID" Type="Int32" /> 
        </DeleteParameters> 
        <UpdateParameters> 
            <asp:Parameter Name="UserID" Type="Int32" /> 
            <asp:Parameter Name="Hospitalname" Type="String" /> 
            <asp:Parameter Name="HospID" Type="Int32" /> 
        </UpdateParameters> 
        <InsertParameters> 
            <asp:Parameter Name="UserID" Type="Int32" /> 
            <asp:Parameter Name="Hospitalname" Type="String" /> 
            <asp:Parameter Name="HospID" Type="Int32" /> 
        </InsertParameters> 
    </asp:SqlDataSource> 
    </form> 
 

1 Answer, 1 is accepted

Sort by
0
GlenB
Top achievements
Rank 1
answered on 17 May 2010, 04:30 AM
Never mind - typical that I worked it out AFTER posting this question:

The solution was to change the UserID in the insert parameters to use the cookie:

        <InsertParameters>
            <asp:CookieParameter CookieName="UserID" Name="UserID" Type="Int32" />
            <asp:Parameter Name="Hospitalname" Type="String" />
        </InsertParameters>



Tags
Grid
Asked by
GlenB
Top achievements
Rank 1
Answers by
GlenB
Top achievements
Rank 1
Share this question
or