Solved

asp.net oracle connection in code behind

Posted on 2009-07-14
9
954 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Note: You must have administrative privileges in order to create/edit Roles. Salesforce.com (http://www.salesforce.com/) (SFDC) is a cloud-based customer relationship management (CRM) system. It is a database most commonly used by sales and marke…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now