Asp.net 在有效的datetime中无法识别字符串

Asp.net 在有效的datetime中无法识别字符串,asp.net,sql-server,Asp.net,Sql Server,我尝试了不同的格式,但仍然给出了这个例外 我正在尝试使用select命令在gridview中显示datatable 我使用SQLServer表中的Expense\u Date列作为日期数据类型 我的密码在这里 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; usi

我尝试了不同的格式,但仍然给出了这个例外

我正在尝试使用select命令在gridview中显示datatable

我使用SQLServer表中的Expense\u Date列作为日期数据类型

我的密码在这里

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

public partial class ConsolidateByMonth : System.Web.UI.Page
{
    double grdTotal = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
protected void btnShowExpenses_Click(object sender, EventArgs e)
{
    string month = DropDownList1.Text;
    string NoOfMonth="";
    string days="";
    switch(month){
        case "January":
            NoOfMonth = "01";
            days = "31";
            break;
        case "Febuary":
            NoOfMonth = "02";
            days = "28";
            break;
        case "March":
            NoOfMonth = "03";
            days = "31";
            break;
        case "April":
            NoOfMonth = "04";
            days = "30";
            break;
         case "May":
            NoOfMonth = "05";
            days = "31";
            break;
        case "June":
            NoOfMonth = "06";
            days = "30";
            break;
        case "July":
            NoOfMonth = "07";
            days = "31";
            break;
        case "August":
            NoOfMonth = "08";
            days = "31";
            break;
         case "September":
            NoOfMonth = "09";
            days = "30";
            break;
        case "October":
            NoOfMonth = "10";
            days = "31";
            break;
        case "November":
            NoOfMonth = "11";
            days = "30";
            break;
        case "December":
            NoOfMonth = "12";
            days = "31";
            break;
    }
    string str1 = (NoOfMonth + "01" + DropDownList2.Text).ToString(); //04012014
    string str2 = (NoOfMonth + days + DropDownList2.Text).ToString();

    DateTime date1 = DateTime.ParseExact(str1, "yyyy-MM-dd", null);
    DateTime date2 = DateTime.ParseExact(str2, "yyyy-MM-dd", null);


    string con_string = "@data source=10.10.10.5; initial catalog= testAzhar; user=xx; password=xxxxx;";
    SqlConnection con = new SqlConnection(con_string);
    string qry = "Select Expense_Category, Expense_Description, Amount from CompanyExpenses3 where Expense_Date>=" + date1 + "and <=" + date2;
    SqlCommand cmd = new SqlCommand(qry, con);
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    GridView1.DataSource = dt;
    con.Close();

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用系统数据;
使用System.Data.SqlClient;
公共部分类ConsolidateByMonth:System.Web.UI.Page
{
双GRD总=0;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效btnShowExpenses\u单击(对象发送者,事件参数e)
{
string month=DropDownList1.Text;
字符串NoOfMonth=“”;
字符串天数=”;
开关(月){
“1月”案:
NoOfMonth=“01”;
天数=“31”;
打破
“2月”一案:
NoOfMonth=“02”;
天数=“28”;
打破
“三月”案:
NoOfMonth=“03”;
天数=“31”;
打破
“四月”案:
NoOfMonth=“04”;
天数=“30”;
打破
“可能”一案:
NoOfMonth=“05”;
天数=“31”;
打破
“6月”案:
NoOfMonth=“06”;
天数=“30”;
打破
“7月”案:
NoOfMonth=“07”;
天数=“31”;
打破
“八月”案:
NoOfMonth=“08”;
天数=“31”;
打破
“9月”案:
NoOfMonth=“09”;
天数=“30”;
打破
“10月”案:
NoOfMonth=“10”;
天数=“31”;
打破
“11月”案:
NoOfMonth=“11”;
天数=“30”;
打破
“12月”一案:
NoOfMonth=“12”;
天数=“31”;
打破
}
字符串str1=(NoOfMonth+“01”+DropDownList2.Text).ToString();//04012014
字符串str2=(NoOfMonth+days+DropDownList2.Text).ToString();
DateTime date1=DateTime.ParseExact(str1,“yyyy-MM-dd”,null);
DateTime date2=DateTime.ParseExact(str2,“yyyy-MM-dd”,null);
string con_string=“@data source=10.10.10.5;初始目录=testAzhar;用户=xx;密码=xxxxx;”;
SqlConnection con=新的SqlConnection(con_字符串);

string qry=“选择费用类别、费用描述、公司费用3中的金额,其中费用日期>=”+date1+”,然后写下如下内容

string str1 = (NoOfMonth + "/01/" + DropDownList2.Text).ToString(); //04012014
string str2 = (NoOfMonth +"/"+ days+"/" + DropDownList2.Text).ToString();

DateTime date1 = DateTime.ParseExact(str1, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime date2 = DateTime.ParseExact(str2, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);

始终使用sql参数(…
where Expense\u Date>=@Expense\u Date
..)而不是字符串连接,以防止sql注入和本地化问题。您应该使用DateTime.DaysInMonth(int,int)以使代码在闰年正常工作,而不是硬编码的switch语句。