Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Database Strucure as follows in Ms Access


fieldname Datatype

Holiday Datetime
Reason Text

Holiday Reason (table Name Leave) In Ms Access
2/20/2013 Labour Day
2/25/2013 Holiday

Holiday save in tehformat of mm/dd/yyyy (month date year format in the database)

When i select the calendar that month all dates will display in datagridview using csharp.

And also user did not able to select the sunday date in that month.

for the above two condition i written a code as follows;


C#
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; 
 
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.Orange;
DGVCalendar.Rows[row].Cells[2].Value = "Sunday";
 
}
dt1 = dt1.AddDays(1);
row++;
}

I want to check another condition selected date in datagridview is present in another Table.

if user select the 2/20/2013 (the above date in datagridview)
using checkbox user select the 2/20/2013 Date,
shows the message "select date is not allowed"

because the date 2/20/2013 is present in the Leave Table.

For the above condition i written the code as follows;

C#
sql = "select format(Holidaydate,'mmm dd yyyy') as Holdate, [Reason] from Tb_Delcared_Holidays where Holidaydate = '" + DGVCalendar.Rows[row].Cells[1].ToString() + "'";
              oledr = GFun.ReadAcessSql(sql);
               while (oledr.Read())
               {
                   DGVCalendar.Rows[row].Cells[1].Style.BackColor = Color.Blue;
                   DGVCalendar.Rows[row].Cells[2].Value = oledr[0].ToString();
               }

Because that date is in Leave Table.

When i run shows error as follows;

Invalid attempt to call Read when reader is closed.

from my above in that condition what is the mistake i made.

[Edit]Code block added[/Edit]
Posted
Updated 22-Feb-13 23:52pm
v2
Comments
Shubh Agrahari 23-Feb-13 5:50am    
this is usually when you try to read by data reader that is no much active in code.....i think problem before it while(oledr.read())..try to put working datareader object....
[no name] 23-Feb-13 6:00am    
so in my above what changes i need to get the correct output


Kindly please help me.

what can i do.

Regards,
Narasiman P.
[no name] 23-Feb-13 6:11am    
please help me reply for my above question
Shubh Agrahari 23-Feb-13 6:36am    
put these your code from starting...
sql = "select format(Holidaydate,'mmm dd yyyy') as Holdate, [Reason] from Tb_Delcared_Holidays where Holidaydate = '" + DGVCalendar.Rows[row].Cells[1].ToString() + "'";
oledr = GFun.ReadAcessSql(sql);
while (oledr.Read())
{
DGVCalendar.Rows[row].Cells[1].Style.BackColor = Color.Blue;
DGVCalendar.Rows[row].Cells[2].Value = oledr[0].ToString();
}

then lets look for error free...
[no name] 23-Feb-13 6:52am    
you said put these your code from starting.

from starting means from my code where i want to put.

please mention where i will put with my above code, i mentioned above.

Kindly please help me.

Regards,
Narasiman P.

please help me.

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