Hello,
I am having a problem with selecting rows from an Access database (for code, I am using c# 2003).
I have a list that I will go through one by one. As I increment through the list, I want to make a select statement to retrieve rows that meet my set criteria. Currrently, I am just outputting the selected column to the screen. However, I get the following error message:
ExecuteReader requires and open and available Connection. The connection's current status is open, Fetching.
What am I doing wrong
int i = 0;
try
{
string strOleDb;
strOleDb = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + myPath;
OleDbConnection cn = new OleDbConnection(strOleDb);
OleDbCommand myCom = new OleDbCommand();
myCom.Connection = cn;
OleDbDataReader dr;
cn.Open();
try{
while (i < fullIndex){
if (myList[i++].bookID > 0){
myCom.CommandText =@"SELECT Employee.EmpEmail
FROM Employee INNER JOIN EmpInterests
ON Employee.EmpID = EmpInterests.EmpID
WHERE ((EmpInterests.EntityID = " + myList
.bookID + ") AND "
+ "((EmpInterests.Interest = '" +thisWord + "') OR "
+ " (EmpInterests.Interest = '0')))";
dr = myCom.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read()){
try{
MessageBox.Show("Email is: " + dr.GetString(0));
}
///end try catch (Exception ex){
MessageBox.Show(ex.Message);
}
//end catch}
//end while}
//end if}
//end whilecn.Close();
}
//end try catch (Exception ex){
MessageBox.Show(ex.Message);
}
//end catch}
//end try catch (Exception ex){
MessageBox.Show(ex.Message);
}
//end catch
Error: ExecuteReader requires and open and available Connection. The connection's current status is open, Fetching.
Venki HR
but isnt that what this statement does:
dr = myCom.ExecuteReader(CommandBehavior.CloseConnection);
Dustpan Dave
Hi boban.s
I tried what you said, to move around my code and it did work to get rid of the error message. The code runs fine, but it does not do what I want it.
where it is at the first point the statement is executed (ie, i = 0) and it will always execute that query for that value. How do I make it take a different value everytime it goes through the while loop
Because I move the select statement outside of the while loop, it captures the value of myList
This is the code I have now:
int i = 0;
try
{
string strOleDb;
strOleDb = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + myPath;
OleDbConnection cn = new OleDbConnection(strOleDb);
OleDbCommand myCom = new OleDbCommand();
myCom.Connection = cn;
OleDbDataReader dr;
cn.Open();
try
.bookID + ") AND "
{
myCom.CommandText = @"SELECT Employee.EmpEmail
FROM Employee, EmpInterests
WHERE ((Employee.EmpID = EmpInterests.EmpID) AND
((EmpInterests.EntityID = "+ myList
+ "((EmpInterests.Interest = '0') OR "
+ " (EmpInterests.Interest = thisWord))))";
while (i < fullIndex
.bookID > 0)
{
if (myList
{
dr = myCom.ExecuteReader();
while (dr.Read())
{
try
{
MessageBox.Show("Email is: " + dr.GetString(0));
}///end try
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}//end catch
}//end while
dr.Close();
}//end if
i++;
}//end while
cn.Close();
}//end try
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}//end catch
}//end try
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}//end catch
JavaBoy
Kimo9909
User parameterized query and change parameter value in the loop.
vandewst
int i = 0;
string strOleDb;
strOleDb = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + myPath;
OleDbConnection cn = new OleDbConnection(strOleDb);
OleDbCommand myCom =
new OleDbCommand(@"SELECT Employee.EmpEmail
FROM Employee, EmpInterests
WHERE ((Employee.EmpID = EmpInterests.EmpID) AND
((EmpInterests.EntityID = AND "
+ "((EmpInterests.Interest = '0') OR "
+ " (EmpInterests.Interest = ))))");
myCom.Parameters.Add("@EntityID", OleDbType.Integer);
myCom.Parameters.Add("@Interest", OleDbType.VarChar, 20).Value = thisWord;
myCom.Connection = cn;
try
{
cn.Open();
while (i < fullIndex)
{
if (myList
.bookID > 0)
{
myCom.Parameters[" EntityID"].Value = myList
.bookID;
OleDbDataReader dr = myCom.ExecuteReader();
while (dr.Read())
{
try
{
MessageBox.Show("Email is: " + dr.GetString(0));
} ///end try
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} //end catch
} //end while
dr.Close();
} //end if
i++;
} //end while
} //end try
catch (OleDbException oldbex)
{
MessageBox.Show(oldbex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} //end catch
finally
{
cn.Close();
}
When you have a loop and you want to use same command but just to change some parameter then you define the command out of the loop and in the loop just set the value of parameters that should be changed in the loop. I put also interest word also as parameter because it is the right way every changeable value the be parameter and you will not wory about type and format of that value. Closing connection should be in finally blok. I just start the exception handling but if you like to easy the way of solving problems you need to have good exception handling, especially when working with database.
Gene Best
Consider closing each reader after you done reading from it to free up connection and use parameterized command (or at least dispose of commands you no longer need).