Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

c# - Setting DateTime as a SqlDataSource parameter for Gridview

Hey I would like to display certain data with my stored procedure for the last 30 days. here is what I have done (aspx.cs file):

 protected void Page_Load(object sender, EventArgs e)     
        {
          DateTime toDate, fromDate;
          toDate = DateTime.Now;

          fromDate = toDate.Subtract(new TimeSpan(31, 0, 0, 0));

          SqlDataSource1.SelectParameters.Add("fromDate", DbType.DateTime, fromDate.ToString());
          SqlDataSource1.SelectParameters.Add("toDate", DbType.DateTime, toDate.ToString());                    

    }

here is my aspx file

 <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" Width="232px" DataKeyNames="CustomerId" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="CreationDate" HeaderText="CreationDate" SortExpression="CreationDate" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SSEnewConnectionString %>"
            SelectCommand="procCustomer_SelectbyCreationDate" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter DbType="DateTime" Name="fromDate" />
                <asp:Parameter DbType="DateTime" Name="toDate" />
            </SelectParameters>
        </asp:SqlDataSource>
        </form>

when I test this my screen comes up blank (other than the masterpage elements) and no errors. any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT: Even though an earlier version of this answer was accepted, it looks like I had misunderstood the parameter type used. The web controls ParameterCollection looks somewhat awful.

I would suggest converting date values to a SQL format (much as that pains me, frankly - string conversions should be avoided as far as possible). For example:

SqlDataSource1.SelectParameters.Add("fromDate", DbType.DateTime,
    fromDate.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
SqlDataSource1.SelectParameters.Add("toDate", DbType.DateTime,
    toDate.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));

(Change to yyyy-MM-dd for date-only types.)

I haven't used SqlDataSource myself, but it also looks like you're introducing the parameters twice - once in the markup, and once in the code. Given that you don't have the values in the markup (including in bindings), you may want to remove them from there - but I could be wrong on that front.

If your query isn't doing what you expect, you should check your database logs (or whatever tool is appropriate) to check what actual query is executing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...