Click here to Skip to main content
15,906,467 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
note it is windows appplciation

using csharp.

C#
sql = "select [Fac_Name],[Holiday_Day] from Tb_common_Holidays where FacName = '" + cb_Faculty_Code.Text.ToString().Trim() + "' and Holiday_Day = '" + dt1.DayOfWeek.ToString().Trim() + "' ";
                oledbdr = GFun.ReadAcessSql(sql);
                oledbdr.Read();
                if (oledbdr.HasRows == true)
                //while (oledbdr.Read())
                {
                    DGVCalendar.Rows[row].Cells[1].Style.BackColor = Color.Pink;
                    DGVCalendar.Rows[row].Cells[2].Style.ForeColor = Color.Brown;
                    DGVCalendar.Rows[row].Cells[2].Value = dt1.DayOfWeek.ToString();
                }
                oledbdr.Close();


in my above code when i run shows error as follows;

Invalid attempt to call Read when reader is closed.


what is the problem in my above code/

please help me.
Posted
Updated 25-Feb-13 22:42pm
v2
Comments
temisegdave 26-Feb-13 5:14am    
is your reader Still Open?

Is the Code outside the scope of the Reader Declared?


===== Oledbdr.Open()====

Let the code above been between "Open" and

===== Oledbdr.Close()======
[no name] 26-Feb-13 5:18am    
it is ms access database.
CHill60 26-Feb-13 5:50am    
I've amended my solution for access instead of sql

is your reader Still Open?

Is the Code outside the scope of the Reader Declared?


===== Oledbdr.Open()====

Let the code above been between "Open" and

===== Oledbdr.Close()======
 
Share this answer
 
It's not clear when you open your reader - it depends what oledbdr = GFun.ReadAcessSql(sql); actually achieves.

I would rather do something like this ...
[Edit - amended for MSAccess instead of SQL]

C#
sql = "select [Fac_Name],[Holiday_Day] from Tb_common_Holidays where FacName = @P1 and Holiday_Day = @P2";

//SqlCommand cmd = new SqlCommand();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = yourConnection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;

// Use parameterised queries - you can google for the many reasons why
//cmd.Parameters.Add("@P1", SqlDbType.VarChar, 1024);
//cmd.Parameters.Add("@P2", SqlDbType.VarChar, 1024);
//cmd.Parameters["@P1"].Value = cb_Faculty_Code.Text.ToString().Trim();
//cmd.Parameters["@P2"].Value = dt1.DayOfWeek.ToString().Trim();

cmd.Parameters["@P1"].AddWithValue("P1", cb_Faculty_Code.Text.ToString().Trim());
cmd.Parameters["@P2"].AddWithValue("P2", dt1.DayOfWeek.ToString().Trim());

//SqlDataReader oledbdr = cmd.ExecuteReader();
OleDbDataReader dr = cmd.ExecuteReader();

oledbdr.Read();
if (oledbdr.HasRows == true)
{
   // Note you are only reading the first row
   DGVCalendar.Rows[row].Cells[1].Style.BackColor = Color.Pink;
   DGVCalendar.Rows[row].Cells[2].Style.ForeColor = Color.Brown;
   DGVCalendar.Rows[row].Cells[2].Value = dt1.DayOfWeek.ToString();
}
oledbdr.Close();
 
Share this answer
 
v2

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