设置日期时间为GridView的一个SqlDataSource参数 [英] Setting DateTime as a SqlDataSource parameter for Gridview

查看:208
本文介绍了设置日期时间为GridView的一个SqlDataSource参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想用我的存储过程来显示某些数据在过去的30天。这里是我做了什么(aspx.cs文件):

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

    }

这是我的aspx文件

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?

推荐答案

编辑:尽管这个答案的早期版本被接受,它看起来像我的理解有误用的参数类型。在<一个href=\"http://msdn.microsoft.com/en-us/library/System.Web.UI.WebControls.ParameterCollection%28v=vs.110%29.aspx\"相对=nofollow> Web控件 ParameterCollection中 看起来有点可怕。

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.

我建议转换日期值到SQL格式(就像那个让我痛苦,坦率地说 - 字符串转换应尽量避免)。例如:

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

(更改为 YYYY-MM-DD 日期只类型。)

我没有使用的SqlDataSource 我自己,但它也看起来的就像你两次引入参数 - 在标记一次,一旦在code。既然你没有值的标记(包括绑定),你可能想从那里删除它们 - 但我可能是错在这一方面

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.

这篇关于设置日期时间为GridView的一个SqlDataSource参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆