I need some guidance for parsing dates. My database table contains values ID (int) and date (datetime: yyyy-mm-dd HH:mm:ss.fff format) and status example
1000 & 2014-02-18 20:32:20.657 & 1
2000 & 2014-02-18 20:32:20.658 & 1
3000 & NULL & -1
I have a C# program that looks at this table for status=1 and date not null and want to insert ID and Date in the same format in a text file. The text file should have
1000 2014-02-18 20:32:20.657
2000 2014-02-18 20:32:20.658
Here's the code.
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString))
{
connection.Open();
SqlCommand cmdSel = new SqlCommand(sqlSelNew, connection);
SqlDataReader reader1 = cmdSel.ExecuteReader();
while (reader1.Read())
{
DataSet ds = GetData(sqlSelNew);
CultureInfo _provider = CultureInfo.InvariantCulture;
ID = Convert.ToInt32(reader1["ID"].ToString());
string dtformat = @"yyyy/MM/dd HH:mm:ss.fff";
var d = DateTime.ParseExact(dateresized,dtformat , _provider);
using (StreamWriter sw = new StreamWriter(txtfilepath, true))
{
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(ID + " " + d);
sw.Flush();
sw.Close();
}
I get "String was not recognized as a valid DateTime." error. How can I handle this? Thanks Rashmi