user2460010
user2460010

Reputation: 27

How do I set the date of a Calendar control from the database?

I have a calendar selection which needs to be populated on page load from a column stored in database.

Example:

Id = 71
String D1= "select date from customer where ID= 71";
Calendar1=???

I have date stored in D1 in string format and Calendar1 is the ID of the calendar. I tried searching google but couldn't find the syntax for assigning that date to calendar1 on page load.

Could you help me in date conversion syntax and assigning it Calendar.

Thanks a lot

Upvotes: 2

Views: 4160

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67928

You can simply set the SelectedDate property of the Calendar control:

Calendar1.SelectedDate = ...;

however, you're going to need to go get that date. You have a SELECT statement there so do something like this:

using (SqlConnection c = new SqlConnection("your connection string"))
{
    c.Open();
    using (SqlCommand cmd = new SqlCommand("select date from customer where ID = @ID", c))
    {
        cmd.Parameters.AddWithValue("@ID", Id);
        var val = cmd.ExecuteScalar();

        Calendar1.SelectedDate = Convert.ToDateTime(val);
    }
}

Upvotes: 2

Related Questions