1

Is it possible to Select Dates from an SQL server and use them to BoldedDates in a monthCalendar? :)

To BoldDates in a monthCalendar I have used following code:

        DateTime[] Reserveret = new DateTime[3];
        Reserveret[0] = new DateTime(2012, 04, 25);
        Reserveret[1] = new DateTime(2012, 04, 01);
        Reserveret[2] = new DateTime(2012, 04, 12);

        monthCalendar1.BoldedDates = Reserveret;

This will Bold those 3 dates in the monthCalendar1.

But instead of hardcoding these dates i would like to select them from my SQL database, that contains 3 columns like this:

        ID       Room           Date
        1        103            2012-04-18
        2        106            2012-04-07
        3        103            2012-04-23
        4        103            2012-04-14
        5        101            2012-04-11

My ultimate goal would be to press forinstance Button103 and then 2012-04-18 & 2012-04-23 & 2012-04-14 would be Bolded in monthCalendar. But im no expert on arrays, which i assume will be needed here :/

2 Answers2

0

Try some thing like this.

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{ 
    for (int i = 0; i < Reserveret .Length; i++)
    {
        if (e.Day.Date == Reserveret ) 
        { 
            e.Cell.Font.Bold = true; 
            //e.Cell.BackColor = System.Drawing.Color.red;  //You may also try this 
        } 
     }
 } 
4b0
  • 21,981
  • 30
  • 95
  • 142
  • Ouch, cant see how this code works. Im working on a C# Windows App in Visual Studio and I dont think its possible to do Cell.Font.Bold on a monthCalendar. Needs something like MonthCalendar1.BoldedDates... – user1348488 Apr 21 '12 at 16:50
0

I gave +1 to this question because I was about to ask for the same, but I found the way to do it, yes, it is possible to do what he's asking.

This article about Arrays and Collections helped me, I used a Generic List to this example because I think it's easier to add items to a dynamic Collection while we are fetching records from database. I changed some of the code to match the question.

//create a class and paste this.

public class createsCollection
{
    private static SqlConnection getConnection()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=yourPCName\SQLEXPRESS;Initial Catalog=..";
        return con;
    }

    public static List <DateTime?> datesList(string room)
    {
      using (var con = new SqlConnection(getConnection().ConnectionString))
      {
        SqlDataReader dReader;
        List <DateTime?> dates = new List<DateTime?>();
        var cad = "SELECT Date from yourTbl where room = @Rn";

        using (var sqlCommand = new SqlCommand(cad, con))
        {
          try
          {   sqlCommand.CommandType = CommandType.Text
              sqlCommand.Parameters.AddWithValue("@Rn", room);
              con.Open();
              dReader = sqlCommand.ExecuteReader();

              if (dReader.HasRows == true)
              {
                while (dReader.Read())
                dates.Add(DateTime.Parse(dReader["Date"].ToString()));
              }
              return dates;
          }
          catch (Exception ex)
          { MessageBox.Show("Error: " + ex.Message);
            return null;
          }
        }
      }
    } 
  }

In your Form:

List<DateTime?> datesLst = new List<DateTime?>();
List<DateTime> notNullableList = new List<DateTime>(); 

private void Form_Load(object sender, EventArgs e)
{
  datesLst = createsCollection.datesList("103"); //this can be changed e.g Button.Text


  //We need to convert the nullable DateTime List 'datesLst' to a non-nullable 
  //DateTime array in order to pass it to the BoldedDates Property.

  if (datesLst != null) //if there were records from the db
  {
      foreach (DateTime? dtm in datesLst)
      {
         string str = dtm.GetValueOrDefault().ToShortDateString();
         DateTime val;

         if (DateTime.TryParse(str, out val))
         {
           notNullableList.Add(val); //add not-null value to the new generic list
         }
       }
       monthCalendar1.BoldedDates = notNullableList.ToArray(); //bold the dates gathered from the db to the Month Calendar
       monthCalendar1.Update();
       monthCalendar1.UpdateBoldedDates();
   }
}

It's a way to do it, and I believe there are better way to implement it but this works pretty good for me when I tested it.

If you want to use a fixed size Array instead of a List, you may want to do a first query that returns the number of (Date) rows based on the specified condition and set it to the array size, also is not necessary to return a nullable List but I did it as my preference. Cheers.

Community
  • 1
  • 1
WhySoSerious
  • 1,930
  • 18
  • 18