Question : Datareader object is already open message

Hello ,

I am trying to get the following code to work.  One of the arguments is a list object.  I am trying to loop through the objects in lstTechCode and return a list object.  When I run the code I get an error message that a datareader object is already open.  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
Public Shared Function GetDatasetList(ByVal strMetricName As String, ByVal strInputCode As String, ByVal lstTechCode As List(Of TechnologySet), ByVal intSelectedYear As Integer) As List(Of DataSetClass)
        Dim datasetlist As New List(Of DataSetClass)
        Dim CnSql As SqlConnection = BadsDB.GetConnection
        Dim cmdA As SqlCommand
        Dim reader As SqlDataReader = Nothing
        cmdA = New SqlClient.SqlCommand("up_GetGraphReportDataset", CnSql)
        cmdA.CommandTimeout = 7200
        Try
            For Each t As TechnologySet In lstTechCode
                With cmdA
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.AddWithValue("@MetricName", strMetricName.ToString)
                    .Parameters.AddWithValue("@InputCode", strInputCode.ToString)
                    .Parameters.AddWithValue("@TechnologySetCode", t.TechnologySetCode.ToString)
                    .Parameters.AddWithValue("@BudgetYear", intSelectedYear)
                End With
                If CnSql.State = ConnectionState.Closed Then
                    CnSql.Open()
                End If
                reader = cmdA.ExecuteReader(CommandBehavior.CloseConnection)
                Dim DataVersionIdOrd As Integer = reader.GetOrdinal("DataVersionId")
                Dim dsc As DataSetClass
                Do While reader.Read
                    dsc = New DataSetClass
                    dsc.DataVersionId = reader.GetString(DataVersionIdOrd)
                    datasetlist.Add(dsc)
                Loop
            Next

            reader.Close()
        Catch excA As SqlException
            MsgBox(excA.Message, excA.GetType.ToString)
        Finally
            CnSql.Close()
        End Try
        Return datasetlist
    End Function

Answer : Datareader object is already open message

try it like this... should work, but not tried because I do not have the storedprocedure etc.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
Public Shared Function GetDatasetList(ByVal strMetricName As String, ByVal strInputCode As String, ByVal lstTechCode As List(Of TechnologySet), ByVal intSelectedYear As Integer) As List(Of DataSetClass)
        Dim datasetlist As New List(Of DataSetClass)
        Dim CnSql As SqlConnection = BadsDB.GetConnection
        Dim cmdA As SqlCommand
        Dim reader As SqlDataReader = Nothing
        cmdA = New SqlClient.SqlCommand("up_GetGraphReportDataset", CnSql)
        cmdA.CommandTimeout = 7200
        Try
            For Each t As TechnologySet In lstTechCode
                With cmdA
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.AddWithValue("@MetricName", strMetricName.ToString)
                    .Parameters.AddWithValue("@InputCode", strInputCode.ToString)
                    .Parameters.AddWithValue("@TechnologySetCode", t.TechnologySetCode.ToString)
                    .Parameters.AddWithValue("@BudgetYear", intSelectedYear)
                End With
                If CnSql.State = ConnectionState.Closed Then
                    CnSql.Open()
                End If
                reader = cmdA.ExecuteReader(CommandBehavior.CloseConnection)
                Dim DataVersionIdOrd As Integer = reader.GetOrdinal("DataVersionId")
                Dim dsc As DataSetClass
                Do While reader.Read
                    dsc = New DataSetClass
                    dsc.DataVersionId = reader.GetString(DataVersionIdOrd)
                    datasetlist.Add(dsc)
                Loop
            reader.Close()
            cmdA.Parameters.Clear()
            Next


        Catch excA As SqlException
            MsgBox(excA.Message, excA.GetType.ToString)
        Finally
            CnSql.Close()
        End Try
        Return datasetlist
    End Function
Random Solutions  
 
programming4us programming4us