Notice that the code itself is almost exactly the same in both examples below.
--CODE 1 (MODULE)
'This code from a Module does NOT work!
----Start code-----
Imports System.Data
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
Module OracleConnect
Public Sub Main()
Try
Dim oradb As String = "Data Source=ddddddd;User Id=uuuuuuu;Password=xxxxxx;"
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandText = "select something from table where field = 'AAAA'"
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
dr.Read()
MsgBox(dr.Item("something"))
conn.Dispose()
Catch ex As Exception
MsgBox("error: " + ex.Message)
Console.WriteLine(ex.Message)
End Try
End Sub
End Module
----End code-----
Error: ORA-12154: TNS:could not resolve the connect identifier specified
******************************************************************
--CODE 2 (FORM)
'This code from a FORM DOES work!
----Start code-----
Imports System.Data
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
Public Class frmMain
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
Dim strHostName As String
Dim strIPAddress As String
Dim oradb As String = "Data Source=ddddddd;User Id=uuuuuuu;Password=xxxxxx;"
Dim conn As New OracleConnection(oradb)
conn.Open()
MsgBox("oradb: " + oradb)
Dim cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandText = "select something from table where field = 'AAAA'"
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
If dr.Read() Then
Label1.Text = dr.Item("something")
Else
Label1.Text = "Source data not found"
End If
Label1.Text = dr.Item("something")
conn.Dispose()
Catch ex As Exception
Me.TextBox1.Text = "Error occurred: " + ex.Message
MessageBox.Show("Error occurred: " + ex.Message)
'Return "Not Connected"
End Try
End Sub
End Class
----End code-----
--CODE 1 (MODULE)
'This code from a Module does NOT work!
----Start code-----
Imports System.Data
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
Module OracleConnect
Public Sub Main()
Try
Dim oradb As String = "Data Source=ddddddd;User Id=uuuuuuu;Password=xxxxxx;"
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandText = "select something from table where field = 'AAAA'"
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
dr.Read()
MsgBox(dr.Item("something"))
conn.Dispose()
Catch ex As Exception
MsgBox("error: " + ex.Message)
Console.WriteLine(ex.Message)
End Try
End Sub
End Module
----End code-----
Error: ORA-12154: TNS:could not resolve the connect identifier specified
******************************************************************
--CODE 2 (FORM)
'This code from a FORM DOES work!
----Start code-----
Imports System.Data
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
Public Class frmMain
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
Dim strHostName As String
Dim strIPAddress As String
Dim oradb As String = "Data Source=ddddddd;User Id=uuuuuuu;Password=xxxxxx;"
Dim conn As New OracleConnection(oradb)
conn.Open()
MsgBox("oradb: " + oradb)
Dim cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandText = "select something from table where field = 'AAAA'"
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
If dr.Read() Then
Label1.Text = dr.Item("something")
Else
Label1.Text = "Source data not found"
End If
Label1.Text = dr.Item("something")
conn.Dispose()
Catch ex As Exception
Me.TextBox1.Text = "Error occurred: " + ex.Message
MessageBox.Show("Error occurred: " + ex.Message)
'Return "Not Connected"
End Try
End Sub
End Class
----End code-----