Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB OracleDataReader

Posted on 2009-07-07
4
Medium Priority
?
477 Views
Last Modified: 2012-05-07
I am selecting a single row that will return a single value. I need to assign that value to a variable of type integer. How can I do this?
The code below is crashing at:
Dim dr As OracleDataReader = myCmd.ExecuteReader()
with this error:
Connection must be open for this operation

I know I just dont have the syntax right because the connection object is correct right before that line is executed.
Dim intDocID As Integer = Nothing
        Dim myConn As Global.Oracle.DataAccess.Client.OracleConnection = Nothing
        Dim myCmd As OracleCommand = Nothing
        Dim strQuery As String = "SELECT DOC_ID FROM DOCUMENTS WHERE DISPLAY_NAME = '1a' 
 
        Try
            Dim strConnect As String = ConfigurationManager.ConnectionStrings("MYDB").ConnectionString
            myConn = New OracleConnection(strConnect)
            myCmd = New OracleCommand(strQuery, myConn)
            myCmd.CommandType = CommandType.Text
            Dim dr As OracleDataReader = myCmd.ExecuteReader() 
            dr.Read()
 
            intDocID = dr.GetInt16("doc_id").ToString()
        Catch ex As Exception
            Dim statusMessage As String = "DATABASE ERROR retrieving data - " + ex.Message
            Throw New ApplicationException(statusMessage)
        Finally
 
            If Not myCmd Is Nothing Then
                myCmd.Dispose()
            End If
 
            If Not myConn Is Nothing Then
                myConn.Close()
                myConn.Dispose()
            End If
        End Try

Open in new window

0
Comment
Question by:bertino12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 24794305
myConn = New OracleConnection(strConnect)
myConn.Open()
0
 
LVL 21

Expert Comment

by:alainbryden
ID: 24794310
You never called myConn.Open(), this is likely the cause of the specific error message you got.

--
Alain
0
 

Author Comment

by:bertino12
ID: 24794345
Well I did it like below in another function and it works fine.
 Dim myDT As DataTable = Nothing
        Dim myConn As Global.Oracle.DataAccess.Client.OracleConnection = Nothing
        Dim myCmd As OracleCommand = Nothing
        Dim myDA As OracleDataAdapter = Nothing
        Dim strQuery As String = "SELECT D.DOC_ID, D.DOC_TYPE, D.DISPLAY_NAME, D.SUMMARY, D.HTML_DOC, D.SORTCODE " & _
            "FROM DOCUMENTS D " 
        Try
            Dim strConnect As String = ConfigurationManager.ConnectionStrings("myDB").ConnectionString
            myConn = New OracleConnection(strConnect)
 
            myCmd = New OracleCommand(strQuery, myConn)
 
            myCmd.CommandType = CommandType.Text
 
            myDA = New OracleDataAdapter(myCmd)
            myDT = New DataTable("FindResults")
 
            myDA.Fill(myDT)
 
        Catch ex As Exception
            Dim statusMessage As String = "DATABASE ERROR retrieving Find data (""Find By Text"") - " + ex.Message
            Throw New ApplicationException(statusMessage)
        Finally
 
            If Not myCmd Is Nothing Then
                myCmd.Dispose()
            End If
 
            If Not myConn Is Nothing Then
                myConn.Close()
                myConn.Dispose()
            End If
        End Try

Open in new window

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 24794446
If you are only returning a single value from 1 row, then use the ExecuteScalar method:

    myCmd.Connection.Open()

    Dim myValue As String = myCmd.ExecuteScalar()

    myCmd.Connection.Close()

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question