Solved

asp.net oracle connection in code behind

Posted on 2009-07-14
9
1,000 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
[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
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

705 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