Search code examples
asp.netgridviewsqldatasource

empty record when binding sqldatasource from c# code


i am trying to bind an sqldatasource from the code by passing variable from another page, but when i run the report i am getting 0 records,

, my code as 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# code

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

did i miss something?


Solution

  • I solved this problem by changing my code

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