如何从gridview中使用数据源和SP使用向导删除数据 [英] How to delete data from gridview which is using datasource and SP using Wizard

查看:149
本文介绍了如何从gridview中使用数据源和SP使用向导删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用gridview来选择,删除和更新数据库中的数据。我已经写了一个SP来完成所有这些操作。基于参数SP决定执行哪个操作。

以下是我的gridview
我的网格图片http://www.freeimagehosting.net/uploads/0a5de50661.jpg

 < asp:GridView ID =GridView1runat =serverDataSourceID =dsDomain
AllowPaging =TrueAllowSorting =TrueAutoGenerateColumns =False
DataKeyNames =DomainId>
<列>
< asp:CommandField ShowDeleteButton =TrueShowEditButton =True/>
< asp:BoundField DataField =DomainIdHeaderText =DomainId
InsertVisible =FalseReadOnly =TrueSortExpression =DomainId>
< / asp:BoundField>
< asp:BoundField DataField =DomainHeaderText =Domain
SortExpression =Domain>
< / asp:BoundField>
< asp:BoundField DataField =DescriptionHeaderText =Description
SortExpression =Description>
< / asp:BoundField>
< asp:BoundField DataField =InsertionDateHeaderText =InsertionDate
SortExpression =InsertionDate>
< / asp:BoundField>
< / asp:GridView>

我使用的数据源在这里

 < asp:SqlDataSource ID =dsDomainrunat =server
ConnectionString =<%$ ConnectionStrings:conLogin%>
SelectCommand =Tags.spOnlineTest_Domain
SelectCommandType =StoredProcedureCancelSelectOnNullParameter =False
DeleteCommand =Tags.spOnlineTest_DomainDeleteCommandType =StoredProcedureOnDeleting =DomainDeleting>

< SelectParameters>

< asp:参数ConvertEmptyStringToNull =trueDefaultValue =Name =DomainIdType =String/>
< asp:参数ConvertEmptyStringToNull =trueDefaultValue =Name =DomainType =String/>
< asp:参数ConvertEmptyStringToNull =trueDefaultValue =Name =DescriptionType =String/>
< asp:Parameter DefaultValue =1Name =OperationTypeType =Byte/>
< / SelectParameters>
< DeleteParameters>
< asp:ControlParameter ControlID =GridView1Name =DomainId
PropertyName =SelectedValueSize =4Type =Int32/>




 < asp:参数ConvertEmptyStringToNull =trueDefaultValue =Name =DomainType =String/> 
< asp:参数ConvertEmptyStringToNull =trueDefaultValue =Name =DescriptionType =String/>
< asp:参数ConvertEmptyStringToNull =trueDefaultValue =4Name =OperationTypeType =Byte/>
< / DeleteParameters>
< / asp:SqlDataSource>


选择操作正常。
但是,当我尝试删除时,它表示
$ b


过程或函数'spOnlineTest_Domain'期望参数'@Domain' ,这是没有提供

但我提供这个参数,作为


我的存储过程调用是这样的



EXEC Tags.spOnlineTest_Domain NULL,NULL,NULL,1 // For Select last parameter is 1
EXEC Tags.spOnlineTest_DomainSelectedRow's DomainId ),NULL,NULL,4 //用于删除最后一个参数将是4



我的过程有4个参数,其中最后一个参数将由程序员设置,它将告诉程序
仅用于选择最后一个参数必须为非空
用于删除第一个和最后一个参数不能为NULL



我的第一个Delete参数是表的主键,我传递这个值,当用户选择一行并点击delete时,我不确定是否使用了Pro pertyName =SelectedValue,我会得到正确的ID值。

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

解决方案

如果您尚未实现ObjectDataSource的OnDeleting事件,请尝试下面的

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

在您的代码背后:

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

//在这种情况下,我假设您的存储过程将DomainId作为参数
paramsFromPage.Remove(Domain);
paramsFromPage.Add(Domain,(int)paramsFromPage [DomainId]);
paramsFromPage.Remove(DomainId);
}

请看 here 获取更多详情。


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

解决方案

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.

这篇关于如何从gridview中使用数据源和SP使用向导删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆