Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Database connections problem

Database connections problem

Scheduled Pinned Locked Moved ASP.NET
databasesysadminhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    beacon dartmouth
    wrote on last edited by
    #1

    I have a page with two drop-down lists, several labels, and a few DB connections. There is a BindData() subroutine that after populating two dropdown lists and a few labels it calls two methods: BindLabels1() and BindLabels2(). The BindData() sub opens and closes a DB connection without problems. The two methods BindLabels1() and BindLabels2() also open and close DB connections, but when they get to execute a data reader, the server throws the following exception: "System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is Closed. at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteReader() " Even though it is clear in the code (if I'm not mistaken) that I do have the statements needed to open and close connections properly, for some reason it doesn't. It's possible I'm opening connections unnecessarily and I should manage it differently. In any case, I'm open to suggestions. Thanx, b-d CODE: Sub Page_Load( s as Object, e as EventArgs ) If Not IsPostBack Then BindData() End If End Sub Sub BindData() Dim conMeta As SqlConnection Dim cmdSelect As SqlCommand Dim drCon As SqlDataReader Dim drDoc As SqlDataReader Dim intApplicationID as Integer = Int32.Parse( Request.QueryString( "id" ) ) conMeta = New SqlConnection( " Server='dbserver';trusted_connection=true;Database ='database'" ) cmdSelect = New SqlCommand( "sDdlContDoc", conMeta ) cmdSelect.CommandType = CommandType.StoredProcedure cmdSelect.Parameters.Add( "@applicationID", intApplicationID ) Try conMeta.open() drCon = cmdselect.ExecuteReader( ) ddlCont.DataSource = drCon ddlCont.DataTextField = "Contact_ID" ddlCont.selectedindex = "0" ddlCont.DataBind() drCon.Close() drDoc = cmdselect.ExecuteReader( ) ddlDocs.DataSource = drDoc ddlDocs.DataTextField = "Document_ID" ddlDocs.selectedindex = "0" ddlDocs.DataBind() Catch exc As Exception Response.Write(exc) Finally If Not drDoc Is Nothing Then drDoc.close() End If cmdSelect = Nothing If conMeta.State = ConnectionState.Open Then conMeta.close() End If conMeta.Dispose() End Try Dim drApp As SqlDataReader conMeta = New SqlConnection( " Server='dbserver';trusted_connection=true;Database ='databaseV2'" ) cmdSelect = New SqlCommand( "sAppGenInfoDetail", conMeta ) cmdSelect.Comman

    K 1 Reply Last reply
    0
    • B beacon dartmouth

      I have a page with two drop-down lists, several labels, and a few DB connections. There is a BindData() subroutine that after populating two dropdown lists and a few labels it calls two methods: BindLabels1() and BindLabels2(). The BindData() sub opens and closes a DB connection without problems. The two methods BindLabels1() and BindLabels2() also open and close DB connections, but when they get to execute a data reader, the server throws the following exception: "System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is Closed. at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteReader() " Even though it is clear in the code (if I'm not mistaken) that I do have the statements needed to open and close connections properly, for some reason it doesn't. It's possible I'm opening connections unnecessarily and I should manage it differently. In any case, I'm open to suggestions. Thanx, b-d CODE: Sub Page_Load( s as Object, e as EventArgs ) If Not IsPostBack Then BindData() End If End Sub Sub BindData() Dim conMeta As SqlConnection Dim cmdSelect As SqlCommand Dim drCon As SqlDataReader Dim drDoc As SqlDataReader Dim intApplicationID as Integer = Int32.Parse( Request.QueryString( "id" ) ) conMeta = New SqlConnection( " Server='dbserver';trusted_connection=true;Database ='database'" ) cmdSelect = New SqlCommand( "sDdlContDoc", conMeta ) cmdSelect.CommandType = CommandType.StoredProcedure cmdSelect.Parameters.Add( "@applicationID", intApplicationID ) Try conMeta.open() drCon = cmdselect.ExecuteReader( ) ddlCont.DataSource = drCon ddlCont.DataTextField = "Contact_ID" ddlCont.selectedindex = "0" ddlCont.DataBind() drCon.Close() drDoc = cmdselect.ExecuteReader( ) ddlDocs.DataSource = drDoc ddlDocs.DataTextField = "Document_ID" ddlDocs.selectedindex = "0" ddlDocs.DataBind() Catch exc As Exception Response.Write(exc) Finally If Not drDoc Is Nothing Then drDoc.close() End If cmdSelect = Nothing If conMeta.State = ConnectionState.Open Then conMeta.close() End If conMeta.Dispose() End Try Dim drApp As SqlDataReader conMeta = New SqlConnection( " Server='dbserver';trusted_connection=true;Database ='databaseV2'" ) cmdSelect = New SqlCommand( "sAppGenInfoDetail", conMeta ) cmdSelect.Comman

      K Offline
      K Offline
      killerslaytanic
      wrote on last edited by
      #2

      Hello: beacon-dartmouth wrote: BindLabels1() and BindLabels2() also open and close DB connections Actually you forgot to open them in both methods. So , write your conMeta.Open() right before calling the ExecuteReader just as you did inside the BindData(). And yes you can ease your life using a single variable for a connection... Create it once, and open it and close it when you need(better to put it in a base page from which inherit all the others with all the common behavior).

      B 1 Reply Last reply
      0
      • K killerslaytanic

        Hello: beacon-dartmouth wrote: BindLabels1() and BindLabels2() also open and close DB connections Actually you forgot to open them in both methods. So , write your conMeta.Open() right before calling the ExecuteReader just as you did inside the BindData(). And yes you can ease your life using a single variable for a connection... Create it once, and open it and close it when you need(better to put it in a base page from which inherit all the others with all the common behavior).

        B Offline
        B Offline
        beacon dartmouth
        wrote on last edited by
        #3

        Thank you killer, You made my day, man. thanx again. I knew there was a more efficient way of handling connectivity but I blanked out. I like your suggestion of using a single variable for a connection. Can you point me to a good example I could look at?? have a good weekend. b-d

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups