2005中GridView 的DataSource中存储过程用到的参数和DataKeyNames的冲突

本文探讨了ASP.NET中GridView控件配置不当导致删除操作出现异常的问题,并提出了两种解决方案:一是调整DataKeyNames属性和DeleteParameters参数以匹配存储过程;二是修改存储过程参数设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<asp:GridView ID="gvEvent" runat="server" CellPadding="4" ForeColor="#4761C0" AllowPaging="True"
         AutoGenerateColumns
="False" DataSourceID="SqlDataSourceforgvEvent"  DataKeyNames="ID,EventDescription" Width="700px" OnPageIndexChanging="gvEvent_PageIndexChanging" OnDataBound="gvEvent_DataBound">
            
<FooterStyle BackColor="#4761C0" Font-Bold="True" ForeColor="White" />
            
<RowStyle BackColor="White" Height="21px" />
            
<EditRowStyle BackColor="#2461BF" />
            
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            
<HeaderStyle BackColor="#4761C0" Font-Bold="True" ForeColor="White" Height="22px" />
            
<AlternatingRowStyle BackColor="White" />
            
<Columns>
            
                
<asp:BoundField DataField="ID" InsertVisible="False" ReadOnly="True"  HeaderText="编号"
                    SortExpression
="ID">
                    
<ItemStyle Width="0px" />
                
</asp:BoundField>
                
<asp:BoundField DataField="PoliceName" HeaderText="派出所"  />
                
<asp:BoundField DataField="PlaceName" HeaderText="报警场所" />
                
<asp:BoundField DataField="EventTime" HeaderText="接警时间" HtmlEncode="False" DataFormatString="{0:yyyy-MM-dd}" />
                
<asp:BoundField DataField="EventParentCategoryName" HeaderText="事件性质" />
                
<asp:BoundField DataField="EventChildCategoryName" HeaderText="事件类别" />
                
<asp:BoundField DataField="EventDescription" HeaderText="事件描述" Visible="False"  >
                    
<ItemStyle CssClass="display:none" />
                
</asp:BoundField>
                
<asp:CommandField HeaderText="操作" ShowDeleteButton="True"  DeleteText="&lt;div id=&quot;de&quot; onclick=&quot;JavaScript:return confirm('确定删除吗?')&quot;&gt;删除&lt;/div&gt;" />
            
</Columns>
        
</asp:GridView>



<asp:SqlDataSource ID="SqlDataSourceforgvEvent" runat="server" ConnectionString="<%$ ConnectionStrings:PoliceManagerConnectionString %>"
            DeleteCommand
="UP_Event_Delete" DeleteCommandType="StoredProcedure" SelectCommand="UP_Event_GetList" SelectCommandType="StoredProcedure">
            
<DeleteParameters>
                
<asp:Parameter Name="ID" Type="Int32" />
                
<asp:Parameter Name="EventDescription" Type="string" />
            
</DeleteParameters>

DataKeyNames   里面有ID和EventDescription

在存储过程中本来只用一个id的参数就可以,可是这样删除的话就会发生异常

要避免异常的发生只有两种方法:

一:把  DataKeyNames里面的EventDescription去掉,只留下一个“ID”;

<DeleteParameters>标签里面只添加一个参数:<asp:Parameter Name="ID" Type="Int32" />

二:在存储过程“UP_Event_Delete”里面定义两个参数,再在<DeleteParameters>标签里面只添加两个参数:<asp:Parameter Name="ID" Type="Int32" /><asp:Parameter Name="EventDescription"

 Type="string" />

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值