Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The connection's current state is closed...MySql db

Status
Not open for further replies.

Maven4Champ

Technical User
Jun 16, 2004
154
Hi,

I am having an issue with the data reader and re-used open connections. I am getting the following error 4 times upon loading of my AddProjections.aspx page.

System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first. at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader) at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Odbc.OdbcCommand.ExecuteReader() at AddProjections.PopulateWeeks()System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first. at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader) at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Odbc.OdbcCommand.ExecuteReader() at AddProjections.PopulatePositions()System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed. at System.Data.Odbc.OdbcConnection.SetStateExecuting(String method, OdbcTransaction transaction) at System.Data.Odbc.OdbcCommand.ValidateConnectionAndTransaction(String method) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader) at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Odbc.OdbcCommand.ExecuteReader() at AddProjections.Page_Load(Object sender, EventArgs e)System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first. at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader) at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Odbc.OdbcCommand.ExecuteReader() at AddProjections.PopulateWeeks()System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first. at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader) at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Odbc.OdbcCommand.ExecuteReader() at AddProjections.PopulatePositions()System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed. at System.Data.Odbc.OdbcConnection.SetStateExecuting(String method, OdbcTransaction transaction) at System.Data.Odbc.OdbcCommand.ValidateConnectionAndTransaction(String method) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader) at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Odbc.OdbcCommand.ExecuteReader() at AddProjections.Page_Load(Object sender, EventArgs e)

Here is my code:
Code:
Imports System.Data
Imports System.Data.Odbc
Imports System.Web.UI.Control
Imports System.Net
Imports System.IO
Imports System
Imports System.Web


Partial Class AddProjections
    Inherits System.Web.UI.Page


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load

        Dim connectionString As String = New String("DRIVER={MySQL ODBC 3.51 Driver};SERVER=mywebdomain.com;DATABASE=MY_DB;UID=USER_NAME;PWD=password;OPTION=3;MultipleActiveResultSets=True;")

        If Not IsPostBack() Then
            PopulateWeeks()
            PopulatePositions()

            'Dim LeagueID As String = Request.QueryString("league").Replace(";", "").Replace("'", "").Trim
            Dim connODBC As New OdbcConnection(connectionString)

            Dim str1, str2, str3 As String
            str1 = "SELECT STATID, PLAYERNAME, POSITIONID, WEEKNUMFK, P_YDS, P_TDS, P_INTS, RU_YDS, RU_TDS, RU_FUM, RC_YDS, RC_REC,"
            str2 = "RC_TDS, RC_FUM, K_XP, K_FG_1_39, K_FG_40_49, K_FG_50_PLS, DEF_ST_PA_0, DEF_ST_PA_2_6, DEF_ST_PA_7_10,"
            str3 = "DEF_ST_PA_11_20, DEF_ST_PA_21_PLS, DEF_ST_SK, DEF_ST_INT, DEF_ST_FUM_REC, DEF_ST_RET_TD FROM TBLSTATISTICS WHERE WEEKNUMFK = '" + weeksDDL.SelectedItem.Text.ToString + "'"
            Dim cmd As OdbcCommand = New OdbcCommand(str1 + str2 + str3, connODBC)

            WEEK_NUM.Text = (weeksDDL.SelectedIndex + 1).ToString

            If connODBC.State <> ConnectionState.Closed Then
                cmd.Connection.Close()
                connODBC.Close()
                cmd.Dispose()
            End If
            Try
                'cmd.Connection.Open()
                gvProjections.DataSource = cmd.ExecuteReader
                gvProjections.DataBind()
                cmd.Connection.Close()
                cmd.Dispose()
            Catch ex As Exception
                Response.Write(ex.ToString)
                cmd.Connection.Close()
                cmd.Dispose()
            End Try

        End If

    End Sub
    Public Sub ImageButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImageButton1.Click

        Dim connectionString As String = New String("DRIVER={MySQL ODBC 3.51 Driver};SERVER=mywebdomain.com;DATABASE=MY_DB;UID=USER_NAME;PWD=password;OPTION=3;MultipleActiveResultSets=True;")

        Dim connODBC1 As New OdbcConnection(connectionString)
        Dim cmd As OdbcCommand = New OdbcCommand("SELECT MAX(STATID)+1 FROM TBLSTATISTICS", connODBC1)
        Dim NewStatID As Integer
        If connODBC1 Is Nothing Then
            Try
                ' cmd.Connection.Open()
                NewStatID = CType(cmd.ExecuteReader.Item(0).ToString, Integer)
                cmd.Connection.Close()
                cmd.Dispose()
            Catch ex As Exception
                Response.Write(ex.ToString)
                cmd.Connection.Close()
                cmd.Dispose()
            End Try
        Else
            cmd.Connection.Close()
            connODBC1.Close()
            cmd.Dispose()
        End If


        Dim uStr1 As String = "INSERT INTO TBLSTATISTICS VALUES('" + NewStatID.ToString + "','" + PLAYER_NAME.Text.ToString + "','" + positionsDDL.SelectedItem.Value.ToString + "','" + (weeksDDL.SelectedIndex + 1).ToString + "','"
        Dim uStr2 As String = P_YDS.Text.ToString + "','" + P_TDS.Text.ToString + "','" + P_INTS.Text.ToString + "','" + RU_YDS.Text.ToString + "','" + RU_TDS.Text.ToString + "','" + RU_FUM.Text.ToString + "','" + REC_YDS.Text.ToString + "','"
        Dim uStr3 As String = REC_RCP.Text.ToString + "','" + REC_TDS.Text.ToString + "','" + REC_FUM.Text.ToString + "','" + K_XP.Text.ToString + "','" + K_FG_1_39.Text.ToString + "','" + K_FG_40_49.Text.ToString + "','" + K_FG_50_PLS.Text.ToString + "','"
        Dim uStr4 As String = DEF_PA_0.Text.ToString + "','" + DEF_PA_2_6.Text.ToString + "','" + DEF_PA_7_10.Text.ToString + "','" + DEF_PA_11_20.Text.ToString + "','" + DEF_PA_21_PLS.Text.ToString + "','" + DEF_SK.Text.ToString + "','" + DEF_INT.Text.ToString + "','"
        Dim uStr5 As String = DEF_FUM_REC.Text.ToString + "','" + DEF_RET_TD.Text.ToString + "')"
        Dim ODBCExecute As OdbcCommand = New OdbcCommand(uStr1 + uStr2 + uStr3 + uStr4 + uStr5, connODBC1)
        If connODBC1.State <> ConnectionState.Closed Then
            cmd.Connection.Close()
            connODBC1.Close()
            cmd.Dispose()
        End If
        Try
            ODBCExecute.Connection.Open()
            ODBCExecute.ExecuteNonQuery()
            ODBCExecute.Connection.Close()
            ODBCExecute.Dispose()
        Catch ex As Exception
            Response.Write(ex.ToString)
            ODBCExecute.Dispose()
        End Try

    End Sub

    Private Sub PopulateWeeks()

        Dim connectionString As String
        connectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=mywebdomain.com;DATABASE=MY_DB;UID=USER_NAME;PWD=password;OPTION=3;MultipleActiveResultSets=True;"

        Dim connODBC2 As New OdbcConnection(connectionString)
        Dim cmd As OdbcCommand = New OdbcCommand("SELECT WeekNum, Description FROM tblweeks", connODBC2)

        If connODBC2.State <> ConnectionState.Closed Then
            cmd.Connection.Close()
            connODBC2.Close()
            cmd.Dispose()
        End If
        Try
            cmd.Connection.Open()
            weeksDDL.DataSource = cmd.ExecuteReader
            weeksDDL.DataBind()
            weeksDDL.DataTextField = cmd.ExecuteReader.Item(1).ToString
            weeksDDL.DataValueField = cmd.ExecuteReader.Item(0).ToString
            cmd.Connection.Close()
            cmd.Dispose()
        Catch ex As Exception
            Response.Write(ex.ToString)
            cmd.Connection.Close()
            cmd.Dispose()
        End Try
        'cmd.Connection.Close()
        'cmd.Connection.Dispose()
        'cmd.Dispose()
    End Sub

    Private Sub PopulatePositions()

        Dim connectionString As String = New String("DRIVER={MySQL ODBC 3.51 Driver};SERVER=mywebdomain.com;DATABASE=MY_DB;UID=USER_NAME;PWD=password;OPTION=3;MultipleActiveResultSets=True;")

        Dim connODBC As New OdbcConnection(connectionString)
        Dim cmd As OdbcCommand = New OdbcCommand("SELECT POSITIONID, ABBREVIATION, DESCRIPTION FROM TBLPOSITIONS", connODBC)
        'Dim positionsDDL As New DropDownList

        'If ConnectionState.Closed = False Then
        '    connODBC.Close()
        '    connODBC.Dispose()
        'End If

        If connODBC.State <> ConnectionState.Closed Then
            cmd.Connection.Close()
            connODBC.Close()
            cmd.Dispose()
        End If
        Try
            cmd.Connection.Open()
            positionsDDL.DataSource = cmd.ExecuteReader
            positionsDDL.DataBind()
            positionsDDL.DataMember = cmd.ExecuteReader.Item(1).ToString
            positionsDDL.DataTextField = cmd.ExecuteReader.Item(1).ToString
            positionsDDL.DataValueField = cmd.ExecuteReader.Item(0).ToString
            cmd.Connection.Close()
            cmd.Dispose()
        Catch ex As Exception
            Response.Write(ex.ToString)
            cmd.Connection.Close()
            cmd.Dispose()
        End Try
        'cmd.Connection.Close()
        'cmd.Connection.Dispose()
        'cmd.Dispose()
    End Sub

    Public Function execUpdate(ByVal p_sql As String) As String
        Dim connectionString As String = New String("DRIVER={MySQL ODBC 3.51 Driver};SERVER=mywebdomain.com;DATABASE=MY_DB;UID=USER_NAME;PWD=password;OPTION=3;MultipleActiveResultSets=True;")
        Dim connODBC As New OdbcConnection(connectionString)
        connODBC.Open()
        Dim ds As DataSet = New DataSet
        Dim da As OdbcDataAdapter = New OdbcDataAdapter
        da.SelectCommand = New OdbcCommand(p_sql, connODBC)
        da.Fill(ds)
        Dim sql_result As String

        'If ConnectionState.Closed = False Then
        '    connODBC.Close()
        '    connODBC.Dispose()
        'End If

        sql_result = ds.Tables.Item(0).Rows(0).Item(0).ToString

        Return sql_result
        connODBC.Close()
        connODBC.Dispose()
    End Function
End Class

Database is MySQL db with Visual Studio 2008 via VB . NET Language.

Any help or ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top