Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 23rd, 2004, 05:21 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default DataReader error

guys I get this error: -

ExecuteReader requires an open and available Connection. The connection's current state is Open, Fetching.

when I try to access my database with this code: -

private void GridDataFiller(object sender, EventArgs e)
{
    string strSmokingFilter = Smoking.SelectedItem.Text.ToString();
    string strAreaFilter = ddlAreas.SelectedItem.Text.ToString();
    string strFurnished;



  lblDescription.Visible = false;
  String strConnect = "Provider = Microsoft.Jet.OleDb.4.0; data source = \\\\premfs3\\sites\\premium8\\adnanarab66\\databas e\\House.mdb;";
  OleDbConnection objConnection = new OleDbConnection(strConnect);

  String strShowDetails = "SELECT * FROM Houses WHERE RentingPrice = ? AND HouseHold = ? AND Smoking = ?";

  OleDbCommand objCommand = new OleDbCommand(strShowDetails, objConnection);

  //objCommand.Connection = objConnection;
  //objCommand.CommandType = CommandType.Text;
  //objCommand.CommandText = strShowDetails;

  objCommand.Parameters.Add("RentingPrice", OleDbType.VarChar, 32, "RentingPrice");
  objCommand.Parameters.Add("HouseHold", OleDbType.VarChar, 32, "HouseHold");
  objCommand.Parameters.Add("Smoking", OleDbType.VarChar, 32, "Smoking");
  //objCommand.Parameters.Add("Furnished", OleDbType.VarChar, 32, "Furnished");

  objCommand.Parameters["RentingPrice"].Value = Payment.Text;
  objCommand.Parameters["HouseHold"].Value = strAreaFilter;
  objCommand.Parameters["Smoking"].Value = strSmokingFilter;

  OleDbDataReader objDataReader = null;
  //objCommand.Parameters["Furnished"].Value = //strFurnished;
  try
  {
      if(objConnection.State != ConnectionState.Open)
    {
    objConnection.Open();
    }
    //else if(objConnection.State == ConnectionState.Open)
    //{
    //objConnection.Close();
    //}
    //objConnection.Open();
      objDataReader = objCommand.ExecuteReader();
          if(objDataReader.HasRows)
          {
              dgHouse.Visible = true;
              dgHouse.DataSource = objCommand.ExecuteReader();
              dgHouse.DataBind();
          }
          else
          {
          Response.Write("This is doing my head in!");
          }
    }
      catch(Exception ex)
      {
    Response.Write(ex.Message);
      Response.End();
      }
      finally
      {
    objDataReader.Close();
    objConnection.Close();
    }
  }

any ideas??

Adz - The World is not enough
__________________
Adz - Learning The J2EE Ways.
 
Old April 23rd, 2004, 06:09 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

A DataReader will block the Connection as long as it is open. This means you cannot execute any other statement over the connection. Your code tries to execute the reader twice:
Code:
objDataReader = objCommand.ExecuteReader();
if(objDataReader.HasRows)
{          
  dgHouse.Visible = true;
  dgHouse.DataSource = objCommand.ExecuteReader();

First you assign the result of the ExecuteReader method to the objDataReader, and then a few lines later, you use ExecuteReader again to fill the datasource. You should assign objDataReader to the DataSource property instead:
Code:
objDataReader = objCommand.ExecuteReader();
if(objDataReader.HasRows)
{          
  dgHouse.Visible = true;
  dgHouse.DataSource = objDataReader;
  This way, you only have one reader open at the time.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Say Goodbye Hollywood by Eminem (Track 8 from the album: The Eminem Show)

 
Old April 24th, 2004, 02:32 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default

nice one man,

ok let me try this again!

Easy

Adz - The World is not enough





Similar Threads
Thread Thread Starter Forum Replies Last Post
datareader in c# ryan.thomson C# 6 June 30th, 2009 12:48 AM
DataReader error on Listing 12-19 cJeffreywang BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 January 3rd, 2008 12:36 PM
datareader MunishBhatia ASP.NET 2.0 Professional 2 October 17th, 2007 07:05 AM
DataReader truongnnhat ASP.NET 1.0 and 1.1 Basics 2 February 18th, 2005 12:41 AM
"There is already an open DataReader " error kaz VS.NET 2002/2003 4 December 17th, 2003 11:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.