How to delete data from gridview which is using datasource and SP using Wizard

橙三吉。 提交于 2019-12-11 18:46:24

问题


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 />


回答1:


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.




回答2:


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();
    }


来源:https://stackoverflow.com/questions/2363973/how-to-delete-data-from-gridview-which-is-using-datasource-and-sp-using-wizard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!