invalid attempt to fieldcount when reader is close

Results 1 to 2 of 2

Thread: invalid attempt to fieldcount when reader is close

  1. #1
    Join Date
    Dec 1969

    Default invalid attempt to fieldcount when reader is close

    Hi, <BR><BR>I have been attempting to write a function that I can pass a SQL string which will return a datareader which I can in turn bind to any dropdownlist control (I am doing this in a code behind page). I keep on encountering the above error on the databind() method of the dropdownlist that I am attempting to bind to. I have included the code that I have written below and wonder if any one might be able to point me in the direction of what I am doing wrong. <BR><BR>// #### Function #### <BR><BR>protected System.Data.SqlClient.SqlDataReader GetDropDown(string strSQL) <BR>{ <BR><BR>SqlDataReader objDataReader = null; <BR><BR>try <BR>{ <BR>objConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); <BR>objCmd = new SqlCommand(strSQL, objConn); <BR>objCmd.Connection.Open(); <BR><BR>objDataReader = objCmd.ExecuteReader(System.Data.CommandBehavior.C loseConnection); <BR>} <BR>catch(SqlException SqlEx) <BR>{ <BR>lblErrLbl.ForeColor = Color.Red; <BR>lblErrLbl.Text = string.Format("SQL Exception: {0}", SqlEx.ToString()); <BR>lblErrLbl.Visible = true; <BR>} <BR>catch(Exception Ex) <BR>{ <BR>lblErrLbl.ForeColor = Color.Red; <BR>lblErrLbl.Text = string.Format("Exception: {0}", Ex.ToString()); <BR>lblErrLbl.Visible = true; <BR>} <BR>finally <BR>{ <BR>if(objDataReader != null) <BR>objDataReader.Close(); <BR>if(objConn != null) <BR>objConn.Close(); <BR>} <BR><BR>return objDataReader; <BR><BR>} <BR><BR>// #### Bind method being used <BR><BR>ddJurisdictionLevel.DataSource = GetDropDown(m_strJurisdictionLevelsSQL); <BR>ddJurisdictionLevel.DataBind(); <BR><BR>// #### Dropdown #### <BR><BR>&#060;asp:dropdownlist id="ddJurisdictionLevel" DataTextField="JurisdictionLevel" DataValueField="CutJurisdictionLevel_ID" runat="server" AutoPostBack="True" /&#062; <BR><BR><BR>If there is any more information that would help resolve this matter let me know. <BR><BR>Cheers, Dan.

  2. #2
    Join Date
    Dec 1969

    Default RE: invalid attempt to fieldcount when reader is c

    You closed the Reader in your operation. You might also find a problem when you try to explicity close the connection in the smae block: you specified it to close for the commandBehavior. You should try removing that "finally" block to get rid of this problems. Or, add more intelligent conditions in that block ("intelligent" - describing the code, not you).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts