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

x
?
Solved

asp.net oracle connection in code behind

Posted on 2009-07-14
9
Medium Priority
?
1,007 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Salesforce.com is a cloud-based customer relationship management (CRM) system. In this article, you will learn how to add and map custom lead and contact fields to your Salesforce instance.
Read about achieving the basic levels of HRIS security in the workplace.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to recover a database from a user managed backup

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