Solved

asp.net oracle connection in code behind

Posted on 2009-07-14
9
983 Views
Last Modified: 2013-12-12
I am trying to connect to oracle on visual web developer express edition 2008 in code behind without using wizards.

I've read the posts but cannot solve the problem below.

When I use the wizards, i connect fine to Oracle.  Below is the web config file with the connection string.

  But when i try and do a code behind and do my own database connection, i get the message

"System.Data.OracleClient' is a namespace and cannot be used as an expression'.

 I also get the same error when i try odbc.

Here is the code:

     Protected Sub Login_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login_Button.Click
        Dim conn_string As String = "<%$ ConnectionStrings:ConnectionString %>"
   
        Using System.Data.OracleClient
 
            OracleConnection(oracleConn = New OracleConnection())
            oracleConn.ConnectionString = "user id=www6026 Data source=edwdev password=Saddy1942"
            oracleConn.Open()
 
             End Using
 
    End Sub
***********************
Here is the connection string from web.config:

        <connectionStrings>
                <add name="ConnectionString" connectionString="Data Source=edwdev;Persist Security Info=True;User ID=www6026;Password=Saddy1942;Unicode=True" providerName="System.Data.OracleClient"/>
        </connectionStrings>
 
 
 
0
Comment
Question by:agw47aa
  • 5
  • 4
9 Comments
 
LVL 15

Expert Comment

by:jinal
ID: 24851123
Also you have to put

Imports System.Data.OracleClient at top of your code behind file

I think you are using vb.net so you have to use Imports System.Data.OracleClient .

 




 
  Protected Sub Login_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login_Button.Click
        Dim conn_string As String =  System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
            Dim  oracleConn as System.Data.OracleClient.OracleConnection = New OracleConnection()
            oracleConn.ConnectionString = conn_string
            oracleConn.Open()
 
             End Using
 
    End Sub

Open in new window

0
 

Author Comment

by:agw47aa
ID: 24851338
I have already coded the import.  I should have included the entire code behind.  Here it is: Please advise.

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Data.OracleClient
 
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim instance As SqlCommand
 
      Protected Sub Login_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login_Button.Click
        Dim conn_string As String = "<%$ ConnectionStrings:ConnectionString %>"
        '      Me.ReadOrderData(conn_string)
 
        Using System.Data.OracleClient
 
            OracleConnection(oracleConn = New OracleConnection())
            oracleConn.ConnectionString = "user id=waq6026 Data source=edwdev password=Sandy1942"
            oracleConn.Open()
 
             End Using
 
    End Sub
End Class
 
0
 
LVL 15

Expert Comment

by:jinal
ID: 24851402

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Data.OracleClient
 
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim instance As SqlCommand
 
      Protected Sub Login_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login_Button.Click
        Dim conn_string As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString")
        '      Me.ReadOrderData(conn_string) 
OracleConnection(oracleConn = New OracleConnection())
oracleConn.ConnectionString = "Data Source=edwdev;Persist Security Info=True;User ID=www6026;Password=Saddy1942;Unicode=True"
oracleConn.Open()
 
             End Using
 
    End Sub
End Class
 

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 15

Expert Comment

by:jinal
ID: 24851410

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Data.OracleClient
 
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim instance As SqlCommand
 
      Protected Sub Login_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login_Button.Click
        Dim conn_string As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString")
        '      Me.ReadOrderData(conn_string) 
Dim oracleConn  as OracleConnection  = New OracleConnection()
oracleConn.ConnectionString = "Data Source=edwdev;Persist Security Info=True;User ID=www6026;Password=Saddy1942;Unicode=True"
oracleConn.Open()
 
             End Using
 
    End Sub
End Class

Open in new window

0
 

Author Comment

by:agw47aa
ID: 24851682
Thanks.. not quite right yet.

First, there is no USING statement but there is an END USING in the code above.  I put the USING statement in.. I still get two errors.

 
Error   1       Value of type 'System.Configuration.ConnectionStringSettings'
cannot be converted to 'String'.
        13      37      C:\...\login_screen\
 
Error   2       'System.Data.OracleClient' is a namespace and
cannot be used as an expression.
        14      15      C:\...\login_screen\
 ***********************************************************************************
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Data.OracleClient
 
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim instance As SqlCommand
 
    Protected Sub Login_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login_Button.Click
        Dim conn_string As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString")
        Using System.Data.OracleClient
 
            OracleConnection(oracleConn = New OracleConnection())
            oracleConn.ConnectionString = "Data Source=edwdev;Persist Security Info=True;User ID=www6026;Password=Saddy1942;Unicode=True"
            oracleConn.Open()
 
        End Using
 
    End Sub
End Class


0
 
LVL 15

Expert Comment

by:jinal
ID: 24851700

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Data.OracleClient
 
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim instance As SqlCommand
 
      Protected Sub Login_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login_Button.Click
        Dim conn_string As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        '      Me.ReadOrderData(conn_string) 
Dim oracleConn  as OracleConnection  = New OracleConnection()
oracleConn.ConnectionString = "Data Source=edwdev;Persist Security Info=True;User ID=www6026;Password=Saddy1942;Unicode=True"
oracleConn.Open()
 
             End Using
 
    End Sub
End Class

Open in new window

0
 

Author Comment

by:agw47aa
ID: 24852527
Thanks to your help, I got it to work.  I also added a data reader and select statement to retrieve some data.  Here is the code with some comments:

' this is the base connection and simple SELECT command
' connects to oracle, and then reads a table and displays the first column in the msgbox.
' there is also logic here to see if any records were returned
'i didn't get the console command to work
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Data.OracleClient
 
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim instance As SqlCommand
 
    Protected Sub Login_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login_Button.Click
        Dim a1 As Integer
 
        Dim oracleConn As OracleConnection = New OracleConnection()
        '  Dim select_Text As String = "select a1, a2 from abc where a1 = 57575"
        Dim select_Text As String = "select a1, a2 from abc"
        Dim cmd As OracleCommand = New OracleCommand()
        a1 = 1
        oracleConn.ConnectionString = "Data Source=edwdev;Persist Security Info=True;User ID=scott;Password=tiger;Unicode=True"
        oracleConn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Connection = oracleConn
        cmd.CommandText = select_Text
 
        '  MsgBox("HELLO ARNIE")
 
        Dim reader As OracleDataReader = cmd.ExecuteReader()
 
        Do While reader.Read()
            MsgBox(reader(0))
            a1 = a1 + 1
            Console.WriteLine(String.Format("{0}, {1}", reader(0), reader(1)))
        Loop
        reader.Close()
        If a1 = 1 Then
            MsgBox("No records found")
        End If
    End Sub
End Class
0
 
LVL 15

Accepted Solution

by:
jinal earned 250 total points
ID: 24855448
So your work in done ?

If you get solution then please select the solution from comment.
0
 

Author Closing Comment

by:agw47aa
ID: 31603326
Great help from Jinal.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

777 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