C# 从字符串转换日期和/或时间时,转换错误失败。尝试生成gridview时
hye,我遇到了一个问题,当我想获取日期“4/12/2013 2:00”的数据时,我会得到一个错误,从字符串转换日期和/或时间时转换失败。但如果我搜索日期“24/11/2013 16:00”,我将能够获得数据输出。我的代码有什么问题吗?因为我已经创建了代码,它应该在下面显示的第一个代码中接受一个日期C# 从字符串转换日期和/或时间时,转换错误失败。尝试生成gridview时,c#,asp.net,sql-server,datagridview,C#,Asp.net,Sql Server,Datagridview,hye,我遇到了一个问题,当我想获取日期“4/12/2013 2:00”的数据时,我会得到一个错误,从字符串转换日期和/或时间时转换失败。但如果我搜索日期“24/11/2013 16:00”,我将能够获得数据输出。我的代码有什么问题吗?因为我已经创建了代码,它应该在下面显示的第一个代码中接受一个日期 if (Dayrange.Checked == true) { if (txtStart1.Text.Length == 10) {
if (Dayrange.Checked == true)
{
if (txtStart1.Text.Length == 10)
{
startdate = DateTime.ParseExact(txtStart1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
}
else if (txtStart1.Text.Length == 9)
{
try
{
startdate = DateTime.ParseExact(txtStart1.Text, "%d/MM/yyyy", CultureInfo.InvariantCulture);
}
catch (FormatException e)
{
startdate = DateTime.ParseExact(txtStart1.Text, "dd/%M/yyyy", CultureInfo.InvariantCulture);
}
}
else if (txtStart1.Text.Length == 8)
{
startdate = DateTime.ParseExact(txtStart1.Text, "%d/%M/yyyy", CultureInfo.InvariantCulture);
}
//check enddate
if (txtEnd1.Text.Length == 10)
{
enddate = DateTime.ParseExact(txtEnd1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
}
else if (txtEnd1.Text.Length == 9)
{
try
{
enddate = DateTime.ParseExact(txtEnd1.Text, "%d/MM/yyyy", CultureInfo.InvariantCulture);
}
catch (FormatException e)
{
enddate = DateTime.ParseExact(txtEnd1.Text, "dd/%M/yyyy", CultureInfo.InvariantCulture);
}
}
else if (txtEnd1.Text.Length == 8)
{
enddate = DateTime.ParseExact(txtEnd1.Text, "%d/%M/yyyy", CultureInfo.InvariantCulture);
}
//check starttime format
if (txtStart2.Text.Length == 1)
{
starttime = DateTime.ParseExact(txtStart2.Text, "%H", CultureInfo.InvariantCulture);
}
else if (txtStart2.Text.Length == 2)
{
starttime = DateTime.ParseExact(txtStart2.Text, "HH", CultureInfo.InvariantCulture);
}
//check endtime format
if (txtEnd2.Text.Length == 1)
{
endtime = DateTime.ParseExact(txtEnd2.Text, "%H", CultureInfo.InvariantCulture);
}
else if (txtEnd2.Text.Length == 2)
{
endtime = DateTime.ParseExact(txtEnd2.Text, "HH", CultureInfo.InvariantCulture);
}
Label1.Text = startdate.ToShortDateString();
Label2.Text = enddate.ToShortDateString();
Label3.Text = starttime.ToShortTimeString();
Label4.Text = endtime.ToShortTimeString();
Label5.Text = startdate.ToShortDateString() + " " + starttime.ToShortTimeString();
Label6.Text = enddate.ToShortDateString() + " " + endtime.ToShortTimeString();
Label7.Text = Label5.Text.Length.ToString();
Label8.Text = Label6.Text.Length.ToString();
if (Label5.Text.Length == 16)
{
combdatetime1 = DateTime.ParseExact(Label5.Text, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
}
else if (Label5.Text.Length == 15)
{
combdatetime1 = DateTime.ParseExact(Label5.Text, "dd/MM/yyyy %H:mm", CultureInfo.InvariantCulture);
}
if (Label6.Text.Length == 16)
{
combdatetime2 = DateTime.ParseExact(Label6.Text, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
}
else if (Label6.Text.Length == 15)
{
combdatetime2 = DateTime.ParseExact(Label6.Text, "dd/MM/yyyy %H:mm", CultureInfo.InvariantCulture);
}
Label9.Text = combdatetime1.ToString();
Label10.Text = combdatetime2.ToShortDateString();
}
下面是将数据绑定到gridview的代码
if ((Byday.Checked == true) || (Dayrange.Checked == true))
{
if (((Maxdata.Checked == true) && (curdata.Checked == true)) && ((txtStart2.Text.Length == 0 ) && (txtEnd2.Text.Length == 0)))
{
gridmaxdata.Visible = true;
lblmaxdata.Visible = true;
lblcurdata.Visible = true;
gridcurdata.Visible = true;
lblmaxdata.Text = "Highest Data";
lblcurdata.Text = "Current Data";
// ConnectionString to NorthWind Database.
string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\shafiq\\Desktop\\history\\App_Data\\Radiation.mdf;Integrated Security=True;User Instance=True";
// Create SQLDataSource.
SqlDataSource sqlDataSource = new SqlDataSource();
sqlDataSource.ID = "SqlDataSource123";
this.Page.Controls.Add(sqlDataSource);
// Bind ConnectionString to SQLDataSource.
sqlDataSource.ConnectionString = connectionString;
// Retrieve records with only 5 Columns from Employees table of NorthWind Database.
sqlDataSource.SelectCommand = "SELECT top 30 [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >= '" + startdate.ToLongDateString() + "') AND ([date] < '" + enddate.ToLongDateString() + "')) ORDER BY [data] DESC, [date] DESC";
// Bind SQLDataSource to GridView after retrieving the records.
gridmaxdata.DataSource = sqlDataSource;
gridmaxdata.DataBind();
// Create SQLDataSource.
SqlDataSource sqlDataSource2 = new SqlDataSource();
sqlDataSource2.ID = "SqlDataSource12";
this.Page.Controls.Add(sqlDataSource2);
// Bind ConnectionString to SQLDataSource.
sqlDataSource2.ConnectionString = connectionString;
// Retrieve records with only 5 Columns from Employees table of NorthWind Database.
sqlDataSource2.SelectCommand = "SELECT [date], [data] FROM [loc1] WHERE (([date] >= '" + startdate.ToLongDateString() + "') AND ([date] < '" + enddate.ToLongDateString() + "')) ORDER BY [data] DESC, [date] DESC";
// Bind SQLDataSource to GridView after retrieving the records.
gridcurdata.DataSource = sqlDataSource2;
gridcurdata.DataBind();
}
else if (((Maxdata.Checked == true) && (curdata.Checked == true)) && ((txtStart2.Text.Length > 0 ) && (txtEnd2.Text.Length > 0)))
{
gridmaxdata.Visible = true;
lblmaxdata.Visible = true;
lblcurdata.Visible = true;
gridcurdata.Visible = true;
lblmaxdata.Text = "Highest Data";
lblcurdata.Text = "Current Data";
// ConnectionString to NorthWind Database.
string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\shafiq\\Desktop\\history\\App_Data\\Radiation.mdf;Integrated Security=True;User Instance=True";
// Create SQLDataSource.
SqlDataSource sqlDataSource = new SqlDataSource();
sqlDataSource.ID = "SqlDataSource123";
this.Page.Controls.Add(sqlDataSource);
// Bind ConnectionString to SQLDataSource.
sqlDataSource.ConnectionString = connectionString;
// Retrieve records with only 5 Columns from Employees table of NorthWind Database.
sqlDataSource.SelectCommand = "SELECT top 30 [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >= '" + combdatetime1.ToLongDateString() + "') AND ([date] < '" + combdatetime2.ToLongDateString()+ "')) ORDER BY [data] DESC, [date] DESC";
// Bind SQLDataSource to GridView after retrieving the records.
gridmaxdata.DataSource = sqlDataSource;
gridmaxdata.DataBind();
// Create SQLDataSource.
SqlDataSource sqlDataSource2 = new SqlDataSource();
sqlDataSource2.ID = "SqlDataSource12";
this.Page.Controls.Add(sqlDataSource2);
// Bind ConnectionString to SQLDataSource.
sqlDataSource2.ConnectionString = connectionString;
// Retrieve records with only 5 Columns from Employees table of NorthWind Database.
sqlDataSource2.SelectCommand = "SELECT [date], [data] FROM [loc1] WHERE (([date] >= '" + combdatetime1.ToLongDateString() + "') AND ([date] < '" + combdatetime2.ToLongDateString() + "')) ORDER BY [data] DESC, [date] DESC";
// Bind SQLDataSource to GridView after retrieving the records.
gridcurdata.DataSource = sqlDataSource2;
gridcurdata.DataBind();
}
if((Byday.Checked==true)| |(Dayrange.Checked==true))
{
如果(((Maxdata.Checked==true)和&&(curdata.Checked==true))&&((txtStart2.Text.Length==0)和&(txtEnd2.Text.Length==0)))
{
gridmaxdata.Visible=true;
lblmaxdata.Visible=true;
lblcurdata.Visible=true;
gridcurdata.Visible=true;
lblmaxdata.Text=“最高数据”;
lblcurdata.Text=“当前数据”;
//连接字符串到NorthWind数据库。
string connectionString=“数据源=。\\SQLEXPRESS;AttachDbFilename=C:\\Users\\shafiq\\Desktop\\history\\App\u Data\\Radiation.mdf;集成安全性=True;用户实例=True”;
//创建SQLDataSource。
SqlDataSource SqlDataSource=新SqlDataSource();
sqlDataSource.ID=“SqlDataSource123”;
this.Page.Controls.Add(sqlDataSource);
//将ConnectionString绑定到SQLDataSource。
sqlDataSource.ConnectionString=ConnectionString;
//从NorthWind数据库的Employees表中检索只有5列的记录。
sqlDataSource.SelectCommand=“从[loc1]中选择前30位[date],[data],其中([data]>='2')、([date]>='”+startdate.ToLongDateString()+”)和([date]<'“+enddate.ToLongDateString()+”)按[data]说明、[date]说明顺序排列”;
//检索记录后,将SQLDataSource绑定到GridView。
gridmaxdata.DataSource=sqlDataSource;
gridmaxdata.DataBind();
//创建SQLDataSource。
SqlDataSource sqlDataSource2=新的SqlDataSource();
sqlDataSource2.ID=“SqlDataSource12”;
this.Page.Controls.Add(sqlDataSource2);
//将ConnectionString绑定到SQLDataSource。
sqlDataSource2.ConnectionString=ConnectionString;
//从NorthWind数据库的Employees表中检索只有5列的记录。
sqlDataSource2.SelectCommand=“从[loc1]中选择[date],[data],其中(([date]>='“+startdate.ToLongDateString()+”)和([date]<'“+enddate.ToLongDateString()+”)按[data]DESC、[date]DESC]排序;
//检索记录后,将SQLDataSource绑定到GridView。
gridcurdata.DataSource=sqlDataSource2;
gridcurdata.DataBind();
}
如果((Maxdata.Checked==true)和(&&&(curdata.Checked==true))&&((txtStart2.Text.Length>0)和(&&(txtEnd2.Text.Length>0)))
{
gridmaxdata.Visible=true;
lblmaxdata.Visible=true;
lblcurdata.Visible=true;
gridcurdata.Visible=true;
lblmaxdata.Text=“最高数据”;
lblcurdata.Text=“当前数据”;
//连接字符串到NorthWind数据库。
string connectionString=“数据源=。\\SQLEXPRESS;AttachDbFilename=C:\\Users\\shafiq\\Desktop\\history\\App\u Data\\Radiation.mdf;集成安全性=True;用户实例=True”;
//创建SQLDataSource。
SqlDataSource SqlDataSource=新SqlDataSource();
sqlDataSource.ID=“SqlDataSource123”;
this.Page.Controls.Add(sqlDataSource);
//将ConnectionString绑定到SQLDataSource。
sqlDataSource.ConnectionString=ConnectionString;
//从NorthWind数据库的Employees表中检索只有5列的记录。
sqlDataSource.SelectCommand=“从[loc1]中选择前30位[date],[data],其中([data]>='2')、([date]>='”“+combdatetime1.ToLongDateString()+”)和([date]<'“+combdatetime2.ToLongDateString()+”)按[data]DESC、[date]DESC]顺序排列;
//检索记录后,将SQLDataSource绑定到GridView。
gridmaxdata.DataSource=sqlDataSource;
gridmaxdata.DataBind();
//创建SQLDataSource。
SqlDataSource sqlDataSource2=新的SqlDataSource();
sqlDataSource2.ID=“SqlDataSource12”;
this.Page.Controls.Add(sqlDataSource2);
//将ConnectionString绑定到SQLDataSource。
sqlDataSource2.ConnectionString=ConnectionString;
//从NorthWind数据库的Employees表中检索只有5列的记录。
sqlDataSource2.SelectCommand=“从[loc1]中选择[date],[data],其中(([date]>=”“+combdatetime1.ToLongDateString()+”)和([date]<”+combdatetime2.ToLongDateString()+”)按[data]说明、[date]说明顺序排列;
//检索记录后,将SQLDataSource绑定到GridView。
gridcurdata.DataSource=sqlDataSource2;
gridcurdata.DataBind();
}
删除格式字符串中的%
如果您需要允许一天,请在下面进行更改
"%d/MM/yyyy"
到
如果输入可以是多种格式,请使用以下重载TryParseExact
DateTime dateValue;
var formatStrings = new string[] { "dd/MM/yyyy", "d/MM/yyyy", "dd/M/yyyy", "d/M/yyyy" };
if (DateTime.TryParseExact(dt, formatStrings, txtStart1.Text, DateTimeStyles.None, out dateValue))
startdate =dateValue;
当转换时间输入时,您可以简单地如下转换它
Label4.Text = string.Format("{0}:00:00",txtEnd2.Text);
您可以检查长度并转换为int值以验证输入
但我想知道为什么所有这些文本框都是“开始日期时间”和“结束日期时间”! 我将使用带有jQueryUIDatePicker的时间选择器
我必须从所有代码中移动%还是只移动几个?因为我使用%来确保输入单个输入(如1)的用户仍然可以接受作为日期/时间。我已尝试删除所有startdate的%
Label4.Text = string.Format("{0}:00:00",txtEnd2.Text);