0

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 http://www.freeimagehosting.net/uploads/0a5de50661.jpg

         <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" />
                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Domain" Type="String" />
                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Description" Type="String" />
                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="4" Name="OperationType" Type="Byte" />
                </DeleteParameters>
             </asp:SqlDataSource>

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.

http://www.freeimagehosting.net/uploads/0a5de50661.jpg />

3
  • two questions : Have you defined "DataKey" in GrdiView ? and Are you using ObjectDataSource ? , If so why not trace it ? Commented Mar 2, 2010 at 15:20
  • Yes, I have defined DataKeyNames="DomainId". Yes I am getting a stack trace. but I am not able to comprehend it. Commented Mar 2, 2010 at 15:39
  • please post your objectdatasource markup Commented Mar 2, 2010 at 15:49

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@madatanic Here is a revision in Question. Plz Check it
Ok, then add Domain into DataKeyNames attribute of your gridview
Why domain has to be addded in datakeynames ?
So that the ObjectDataSource knows what value to pick up
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
1

u can write:(between gridView tags) \< asp:TemplateField ShowHeader="false"> \asp:LinkButton ID="linkButton1" runat="server" CausesValidation="false" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" Text="Delete" />

     </ItemTemplate>

     </asp:TemplateField>

and after u can implement the "GridView1_RowDeleting" methode,like that: protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {

        //makeDelete("YourStoredProcedure");
        //mydataBindGV();
    }

1 Comment

sorry for the \< and the \ but i couldn't fix them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.