views:

407

answers:

1

I am using a gridview to select, delete and update data in database. I have written a single SP for doing all these operation. Based on a parameter SP decides which operation to perform.

Here is the image of my gridview image of my grid

         <asp:GridView ID="GridView1" runat="server" DataSourceID="dsDomain" 
            AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
            DataKeyNames="DomainId" >
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="DomainId" HeaderText="DomainId" 
                    InsertVisible="False" ReadOnly="True" SortExpression="DomainId">
                </asp:BoundField>
                <asp:BoundField DataField="Domain" HeaderText="Domain" 
                    SortExpression="Domain">
                </asp:BoundField>
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" >
                </asp:BoundField>                            
                <asp:BoundField DataField="InsertionDate" HeaderText="InsertionDate" 
                    SortExpression="InsertionDate">
                </asp:BoundField>                            
        </asp:GridView> 

Data Source that I am using is here

           <asp:SqlDataSource ID="dsDomain" runat="server" 
                ConnectionString="<%$ ConnectionStrings:conLogin %>" 
                SelectCommand="Tags.spOnlineTest_Domain" 
                SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False"
                        DeleteCommand="Tags.spOnlineTest_Domain" DeleteCommandType="StoredProcedure" OnDeleting="DomainDeleting">

                <SelectParameters>

                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="DomainId" Type="String" />
                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Domain" Type="String" />
                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Description" Type="String" />
                    <asp:Parameter  DefaultValue="1" Name="OperationType" Type="Byte" />
                </SelectParameters>
                <DeleteParameters>
                     <asp:ControlParameter ControlID="GridView1" Name="DomainId" 
                        PropertyName="SelectedValue" Size="4" Type="Int32" />

Select operation is working fine. But when I tried to delete, it says

Procedure or Function 'spOnlineTest_Domain' expects parameter '@Domain', which was not supplied
But I am supplying this parameter, as

My Stored procedure calling is like this

EXEC Tags.spOnlineTest_Domain NULL, NULL, NULL, 1 // For Select last parameter will be 1 EXEC Tags.spOnlineTest_Domain "SelectedRow's DomainId), NULL, NULL, 4 // For Delete last parameter will be 4

My procedure has 4 parameters where last parameter will be set by programmer which will tell the program for what kind of operation to be performed. For Select only last parameter has to be Not Null. For Delete first and last parameter cannot be NULL.

My first Delete parameter is Primary key of the table. I am passing this value, when a user selects a row and hit delete. I am not sure by using PropertyName="SelectedValue", will I get the right value of the ID.

+1  A: 

If you have not implemented OnDeleting event of the ObjectDataSource, try the below

<asp:ObjectDataSource .... OnDeleting="YourDeletingEvent" ...
</asp:ObjectDataSource>

In your code behind:

private void YourDeletingEvent(object source, ObjectDataSourceMethodEventArgs e)
{
  IDictionary paramsFromPage = e.InputParameters;

  //In this case I assume your stored procedure is taking a DomainId as a parameter
  paramsFromPage.Remove("Domain");
  paramsFromPage.Add("Domain", (int)paramsFromPage["DomainId"]);
  paramsFromPage.Remove("DomainId");
}

Please look here for more details.

madatanic
@madatanic Here is a revision in Question. Plz Check it
vaibhav
Ok, then add Domain into DataKeyNames attribute of your gridview
madatanic
@madatanic Why domain has to be addded in datakeynames ?
vaibhav
So that the ObjectDataSource knows what value to pick up
madatanic
Thx for help madatnic your solution worked for me, although i was expecting it to get worked for me from frontent only, without using code behind file. +1 for solution. But Q is still open for my actual desired Solution
vaibhav