无效的案例例外

时间:2016-09-08 05:02:34

标签: linq datetime datatable dropdown

在报告页面中,我有下拉列表和datepicker fromdate和todate以及按钮 所以当我从下拉列表和日期选择器中选择值时,显示错误

在这一行

dt=report(Convert.ToDateTime(fromdate), Convert.ToDateTime(todate), Convert.ToString(DropDownList1.SelectedValue));

错误

An exception of type 'System.InvalidCastException' occurred in mscorlib.dll but was not handled in user code

Additional information: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.IConvertible'

  protected void Button1_Click(object sender, EventArgs e)
    {

        dt=report(Convert.ToDateTime(fromdate), Convert.ToDateTime(todate), Convert.ToString(DropDownList1.SelectedValue));
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    DataTable dt = new DataTable();
    public DataTable report(DateTime fromdate,DateTime todate,string IMEI)
    {

    DateTime fromdatee = Convert.ToDateTime(Request.Form["fromdate"]);
        DateTime todatee = Convert.ToDateTime(Request.Form["todate"]);

        Entities track = new Entities();

        DateTime fr_date = new DateTime(fromdatee.Year, fromdatee.Month, fromdatee.Day, 0, 0, 0);
        DateTime t_date = new DateTime(todatee.Year, todatee.Month, todatee.Day, 23, 59, 59);

        List<spGetReport_Result> report = track.spGetReport(IMEI,fr_date,t_date).ToList();
        dt.Columns.Add("Time",typeof(DateTime));
        dt.Columns.Add("X",typeof(float));
        dt.Columns.Add("valuenumber",typeof(int));

        foreach(var c in report)
        {
            dt.Rows.Add(c.Time, c.X, c.valuenumber);
        }
        return dt;

    }

HTML

      <form id="form1" runat="server">
    <div>
        <span>
            <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
        </span>
        <span>
      <input  id="fromdate"    runat="server" clientidmode="static" />
        </span>
        <span>
     <input id="todate"   runat="server" clientidmode="static" />

        </span>
        <span>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </span><br />


                 <asp:Label ID="Label1" style="margin-left: 220px;" runat="server" Text="Export to"></asp:Label>
                  <asp:GridView ID="GridView1" runat="server" class="display nowrap"
        Width="100%" CellPadding="0"
        Font-Names="Verdana" BackColor ="White" BorderColor="#CCCCCC" BorderStyle="None" 
        BorderWidth="1px" Font-Size="9pt">
    <FooterStyle BackColor="White" ForeColor="#000066" />
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
    <RowStyle ForeColor="#000066" />
    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#007DBB" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
    </div>
    </form>

1 个答案:

答案 0 :(得分:0)

因为您要发送Convert.ToDateTime HTML控件而不是要转换的字符串。

你应该这样做:

dt = report(Convert.ToDateTime(fromdate.Value), Convert.ToDateTime(todate.Value), DropDownList1.SelectedValue);

一个非常简单的调试过程会很快向您展示问题所在。

DropDownList1.SelectedValue已经是一个字符串,因此无需转换它。

无论如何,您应首先使用验证器检查这些输入中的内容是否真正有效DateTime表示。