简体   繁体   中英

How to convert a date string for the Calendar control?

I'm using the Calendar control, but I've a problem. In particular I stay iterating through a database for get all dates available into a table; like this:

public void setFixturesControl()
{
        m_dbConnection.Open();
        string query = "select * from dates";
        SQLiteCommand input = new SQLiteCommand(query, Database.m_dbConnection);
        SQLiteDataReader reader = input.ExecuteReader();
        ...

I want add multiple dates in the control, but in this line:

while (reader.Read()) 
{

   MainWindow.AppWindow.Calendar.SelectedDates.Add(new DateTime(reader["data"].ToString()));
 }

I get this error:

Can't convert from string to long

What I doing wrong?

If reader["data"] is already of type DateTime , then you just need to remove the ToString() call.

while (reader.Read()) 
{    
   MainWindow.AppWindow.Calendar.SelectedDates.Add((DateTime)reader["data"]);
}

If reader["data"] is not a DateTime, then you have to convert it. DateTime doesn't have a consutrctor that takes a String. You need to Parse it, instead:

while (reader.Read()) 
{    
   MainWindow.AppWindow.Calendar.SelectedDates.Add(DateTime.Parse(reader["data"].ToString()));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM