2

I'm trying to access some data from SQL Server in ASP.NET, but I can't get a parameter to pass to a SQL Server stored procedure. The parameter is located in the URL (i.e. www.company.com/ImportantManager.aspx?id=Jones,%Bob), as I only want Bob Jones' results displayed.

NiceMgr_Total is the stored procedure. I have seen many different responses to similar questions on here and the Microsoft forums, but I think the stored procedure might be messing it up (the @manager parameter specifically - the other parameters seem to work fine).

I've tried it multiple ways, and I'm getting an assortment of parser errors and "parameter not found" errors. Thanks for your help.

Here's the code:

<asp:GridView ID="GridView1" DataSourceID="SqlDataSource7" runat="server" 
       AutoGenerateRows="False" EmptyDataText="There are no data records to display."
       AutoGenerateColumns="False">
   <Columns>
       <asp:BoundField DataField="Manager" HeaderText="Manager" SortExpression="Manager" 
            ItemStyle-Width="41px" ItemStyle-HorizontalAlign="Center" 
            HeaderStyle-CssClass="tableheader3" ItemStyle-CssClass="tabledata2" />
   </Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource7" runat="server" 
     ConnectionString="<%$ConnectionStrings:ConnectionString3 %>" 
     ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>" 
     SelectCommand="DECLARE @PerDate VARCHAR(50)
    DECLARE @CurrentDate DATETIME
    SET @CurrentDate = (SELECT MAX([MostRecent]) FROM sameDB.dbo.RecentDate)
    SET @PerDate = CAST(CONVERT(VARCHAR(10),@CurrentDate,120) AS VARCHAR(50))
    EXEC [NiceMgr_Total] @date1=@PerDate, @date2=@PerDate, @manager=@MgrName">

    <selectparameters>
        <asp:querystringparameter name="MgrName" DBtype="String" 
             QueryStringField="<%Response.Write(Request.QueryString.Item("id"))%>" />
    </selectparameters>
</asp:SqlDataSource>
4

2 回答 2

1

您的方法存在很多问题 - 请尝试以下步骤:

  1. 需要在您的SelectCommand
  2. 您需要将所有参数放入<SelectParameters>集合中
  3. 请:如果你有日期 - 然后使用DATEorDATETIME数据类型,不要经常来回转换字符串!
  4. 您需要告诉您的选择命令它是一个存储过程(而不是内联 SQL 文本)
  5. 为您<QueryStringParameter>- 您只需要指定查询字符串元素的名称SqlDataSource-将读取值本身

你需要尝试这样的事情:

<asp:SqlDataSource ID="SqlDataSource7" runat="server" 
        ConnectionString="<%$ConnectionStrings:ConnectionString3 %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>" 
        SelectCommand="dbo.NiceMgr_Total"   <!-- *JUST* the name! -->
        SelectCommandType="StoredProcedure">   <!-- tell it's a stored procedure! -->
    <SelectParameters>
        <asp:QueryStringParameter name="MgrName" DbType="String" QueryStringField="id" />
        <!-- specify *ALL* parameters here! -->
        <asp:Parameter Name="date1" DbType="DATETIME" />   
        <asp:Parameter Name="date2" DbType="DATETIME" />
    </SelectParameters>
</asp:SqlDataSource>

现在,当然,您需要找到某种方法来指定date1anddate2参数,然后再SelectCommand执行SqlDataSource...。

于 2013-07-16T17:10:58.900 回答
-1

尝试:

  QueryStringField="<%Response.Write(HttpUtility.HtmlDecode(Request.QueryString.Item("id")))%>" />

您可能需要先对查询字符串进行解码,然后才能对其进行参数化。

于 2013-07-16T17:02:38.910 回答