从c#代码绑定sqldatasource时的空记录

时间:2012-08-02 09:02:58

标签: asp.net gridview sqldatasource

我试图通过从另一个页面传递变量来从代码绑定sqldatasource,但是当我运行报告时我得到0条记录,

,我的代码为fllow

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:WorkflowConnStr %>"
    SelectCommand="SELECT * FROM tbl_ServiceTracking WHERE (ActorUID = @strUserId) AND (RequestedDate &gt;= @DateFrom) AND (RequestedDate  &lt;= @DateTo)">
    <SelectParameters>
        <asp:Parameter Name="strUserId" />
        <asp:Parameter Name="DateFrom" />
        <asp:Parameter Name="DateTo" />
    </SelectParameters>
</asp:SqlDataSource>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" />

c#c​​ode

string strUserId = Request.QueryString["userid"];
string DateFrom = Request.QueryString["dtfrom"];
string DateTo = Request.QueryString["dtto"];

SqlDataSource1.SelectParameters.Add("strUserId", strUserId);
SqlDataSource1.SelectParameters.Add("DateFrom", DateFrom);
SqlDataSource1.SelectParameters.Add("DateTo", DateTo);
SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataReader;
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();

我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

我通过更改代码解决了这个问题

SqlDataSource1.SelectParameters["strUserId"].DefaultValue = strUserId;
SqlDataSource1.SelectParameters["DateFrom"].DefaultValue = DateFrom;
SqlDataSource1.SelectParameters["DateTo"].DefaultValue = DateTo;

答案 1 :(得分:0)

您还可以在设计模式中设置选择参数:

<SelectParameters>
  <asp:QueryStringParameter Name="UserID" QueryStringField="userid" 
    Type="String" DefaultValue="userid" />
  <asp:QueryStringParameter DefaultValue="dtfrom" Name="dtfrom" 
    QueryStringField="dtfrom" Type="String" />
  <asp:QueryStringParameter DefaultValue="dtto" Name="dtto" 
    QueryStringField="dtto" Type="String" />
</SelectParameters>