问题描述
是的,我有问题,当我想获取日期数据时4/12/2013 2:00我将收到错误,转换失败时从字符串转换日期和/或时间。但如果我搜索日期24/11/2013 16:00,我将能够获得数据输出。我的代码有什么问题,因为我已经创建了代码,它应该在下面显示的第一个代码中接受一个日期。
hye, i'm having problem where when i want to get data for date "4/12/2013 2:00" i will get an error,Conversion failed when converting date and/or time from character string. but if i search for date "24/11/2013 16:00", i would be able to get the data output. is there anything wrong with my code since i have already create code for which it should have accept a single date in the first code shown below.
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的代码。
here is the code to bind the data to 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();
}
推荐答案
private string dtString = "4/12/2013 2:00";
//somewhere in a method
DateTime theDate = DateTime.Parse(dtString, CultureInfo.InvariantCulture);
但是我不知道这是否解决了绑定到GridView的问题。
I do not know if this solves the problem with binding to the GridView, however.
这篇关于从字符串转换日期和/或时间时转换错误失败。在尝试生成gridview时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!