Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Data Base Design as follows in Ms Access ;

Table Name (Tb_Delcared_Holidays)
HolidayDate Datetime
Reason Text

In Database Records as follows;

HolidayDate Reason
2/22/2013 Holiday
2/25/2013 Holiday

Design as follows;

Faculty Code combobox
Calendar (month Calendar)

Datagrdiview


In combobox all faculty code retrieved from the table and display in to the Combobox.

when i select the calendar that month all date is display in to the datagridview .

when i select the Feburary month in calendar that month all date display in datagridview.

I want to check one condition,when display the date in datagrdiview,

For february month the below two dates is holiday in the Leave Table.
2/22/2013 Leave
2/25/2013 Leave

When i display the date in datagridview,for the above dates in datagridview, the user did not able to select the two dates


Code as follows;
C#
private void Faculty_Available_Calendar_DateChanged(object sender, DateRangeEventArgs e)
{
    //selected month all dates  display in datagridiview
    DGVCalendar.Rows.Clear();
    DateTime dt1 = Faculty_Available_Calendar.SelectionStart;
    dt1 = new DateTime(dt1.Year, dt1.Month, 1);
    DateTime dt2 = dt1.AddMonths(1);
    int numDays = (dt2 - dt1).Days;

    if (DGVCalendar.RowCount < numDays)
    {
        DGVCalendar.RowCount = numDays;
    }

    int row = 0;
    while (dt1 < dt2)
    {
        DGVCalendar.Rows[row].Cells[1].Value = dt1.ToString("dd-MMM-yyyy");
        DGVCalendar.Rows[row].Cells[0].Value = true;  //All checkbox Checked Defaulty 
        if (dt1.DayOfWeek == DayOfWeek.Sunday)
        {
            DGVCalendar.Rows[row].ReadOnly = true;
            DGVCalendar.Rows[row].Cells[0].Value = false;
            DGVCalendar.Rows[row].Cells[0].Style.BackColor = Color.Orange;
            DGVCalendar.Rows[row].Cells[1].Style.BackColor = Color.Orange;
            DGVCalendar.Rows[row].Cells[2].Style.BackColor = Color.Yellow;
            DGVCalendar.Rows[row].Cells[2].Style.ForeColor = Color.Red;
            DGVCalendar.Rows[row].Cells[2].Value = "Sunday";
        }

        sql = "select format(HolidayDate   ,'mm dd yyyy') as Holdate, [Reason] from Tb_Delcared_Holidays where Holidaydate = '" + DGVCalendar.Rows[row].Cells[1].ToString() + "'";
        dr = GFun.ReadAcessSql(sql);
        dr.Read();
        if (dr.HasRows == true)
        {
            DGVCalendar.Rows[row].Cells[1].Style.BackColor = Color.Blue;
            DGVCalendar.Rows[row].Cells[2].Value = dr[0].ToString();
        }

        dt1 = dt1.AddDays(1);
        row++;
    }
}

the above code, in the datagridview for the selected month date, display in datagridview,if the
any of the date is holiday in Tb_Declared_Holidays table,

that date should be shown in different color,user should not select the date.

from my above when i run error shows as follows.
Invalid attempt to call read when reader is closed.

the above shows in the below line as follows;
C#
sql = "select format(HolidayDate   ,'mm dd yyyy') as Holdate, [Reason] from Tb_Delcared_Holidays where Holidaydate = '" + DGVCalendar.Rows[row].Cells[1].ToString() + "'";
dr = GFun.ReadAcessSql(sql);
dr.Read(); [Invalid attempt to call  read when reader is closed]
if (dr.HasRows == true)
{
    DGVCalendar.Rows[row].Cells[1].Style.BackColor = Color.Blue;
    DGVCalendar.Rows[row].Cells[2].Value = dr[0].ToString();
}

dr.read(); line error shows Invalid attempt to call read when reader is closed.

what is the problem in my above code please help me.
Posted
Updated 24-Feb-13 16:33pm
v3
Comments
sahabiswarup 24-Feb-13 23:03pm    
Good question;
I've worked with one one date and in that case i'm just using Maxdate and Mindate; but in your case that is not a working at al. You make store holiday dates in a datatable, and create an event while selecting date there will a popup message if that date exists in that datatable.
If you found any better solution please share with us.
[no name] 25-Feb-13 0:14am    
u said store the holiday dates in datatable, and create an event while selecting date there will be pop up message if that date exists in that datatable.

for the above how can i do using csharp.

note it is windows aplication.
[no name] 25-Feb-13 0:48am    
please replay for my above question please help me.
Shubh Agrahari 25-Feb-13 1:29am    
tell me one thing still the error that "Invalid attempt to call read when reader is closed" error occurring or not...?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900