How to set Custom text on Calendar control based on SQL query result

137 Views Asked by At

My application is an ASP.Ne/C# project using NPGSQL and POSTGRESQL. I have a calendar control on a web page named deskCal.aspx On the OnDayRender event I have the following:

protected void CalRender(object sender, DayRenderEventArgs e)
    { 
    if (e.Day.Date == new DateTime(2018,12,11))
        {
            Literal TheLit = new Literal();
            TheLit.Text = "<br> X";
            e.Cell.Controls.AddAt(1, TheLit);
        }

    }

That puts a "X" in the cell for Dec, 11 2018 Like this:
Value for Dec 11 2018
Now, I would like to have the same result but rather than put in the specific date I want to get the value from a SQL query. However, I am not sure how to do that. Here is what I have so far:

    protected void CalRender(object sender, DayRenderEventArgs e)
        { 
             NpgsqlConnection con = new NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MydatabBase"].ConnectionString);
             string SelDate = "SELECT thedate FROM tbldateholder WHERE thedate = '2018-11-12' "
             NpgsqlCommand getthedate = new NpgsqlCommand(SelDate,con)

              if (e.Day.Date == NOT SURE WHAT TO DO HERE)
              {
                Literal TheLit = new Literal();
                TheLit.Text = "<br> X";
                e.Cell.Controls.AddAt(1, TheLit);
              }

          }

Should I be doing a reader then pass that result?

1

There are 1 best solutions below

0
On BEST ANSWER

To do this based on a SQL query this is what works for me.

 protected void CalRender(object sender, DayRenderEventArgs e)
        {
            con.Open();
            string  cmdRead = "SELECT theday FROM mytable Where theday = '2018-12-11'";
            NpgsqlCommand thedate = new NpgsqlCommand(cmdRead, con);
            var seeme = thedate.ExecuteScalar();
            DateTime dt = Convert.ToDateTime(seeme);
            if (e.Day.Date == dt)
                {
                    Literal TheLit = new Literal();
                    TheLit.Text = "<br> X";
                    e.Cell.Controls.AddAt(1, TheLit);
                }
            con.Close();

        }