<%@ Impo...">

ODBC Connection Attempt - Network Path Not Found

I am trying to update a database (Oracle through ODBC) in ASP:

<%@ Page LANGUAGE="VB"%> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Configuration" %> <% Dim objConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ODBCNAME").ConnectionString) 'example query Dim strSQL As String = "update foo set bar='BAZ'" objConn.Open() Dim objCmd As New SqlCommand(strSQL, objConn) Try objCmd.ExecuteNonQuery() Response.Write("Record updated") Catch e As Exception Response.Write(e.ToString) End Try %> 

Where web.config has:

 <connectionStrings> <add name="ODBCNAME" connectionString="server=ExampleServerName;" providerName="System.Data.Odbc" /> </connectionStrings> 

When I do this, I get an error in the objCOnn.Open () line:

 System.ComponentModel.Win32Exception: The network path was not found. 

An ODBC data source is created, tested and works with other standalone applications (closed source), and I can test the connection using the ODBC settings that work.

How can I connect to this database from my code? Or how can I debug this problem?

+6
source share
2 answers

Oracle can be a tricky beast.

First use OdbcConnection instead of SqlConnection if you are using ODBC.

See documents from Microsoft

Sample code adapted using the MSDN link:

 Private Sub InsertRow(ByVal connectionString As String) Dim queryString As String = _ "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')" Dim command As New OdbcCommand(queryString) Using connection As New OdbcConnection(connectionString) command.Connection = connection connection.Open() command.ExecuteNonQuery() ' The connection is automatically closed at ' the end of the Using block. End Using End Sub Dim connectionString as String = <your connection details here> InsertRow(connectionString) 

Further, I recommend using the Oracle Data Provider for .NET (ODP.NET, AKA Oracle.DataAccess.dll ), which is provided with Oracle Client (although it is not always installed by default, you may need to perform a custom installation) works better than ODBC. and can use the functionality of Oracle. However, if you want to use ODBC (and, of course, there are some good reasons to keep it common), read on:

In addition to other suggestions, make sure the architecture is consistent. This only applies to you if you are in a 64-bit box.

If a 64-bit OS and a 64-bit application must use 64-bit ODBC to determine the DSN C:\Windows\System32\odcad32.exe

If a 64-bit OS and 32-bit application must use 32-bit ODBC to determine the DSN C:\Windows\SysWOW64\odbcad32.exe

(Yes, the names are REALLY confusing! SysWOW64 basically means emulating 32-bit in a 64-bit enviro. Another problem is Microsoft first called the thing odbcad32 . Maybe odbcad would be better, but they probably should have differentiated 32-bit and 16-bit version at the time .. just a guess)

If it is a 32-bit OS, then the location of odbcad32.exe is the default location (the same as 64-bit on 64-bit) and defaults to PATH .

If your application will run in 32-bit and 64-bit, you must define two DSNs, one for 32 and one for 64.

Alternatively, you can customize the architecture of your project in the project settings. By default, it can be Any CPU , which (I think) means a preference for the native .. so if you build on a 64-bit OS, you get a 64-bit version of exe if you do not change it to x86 or something else. In Visual Studio 2015, you can also use the prefer 32-bit checkbox.

And the same goes for the Oracle client: you must use the appropriate architecture.

+2
source

If you are trying to use ODBC, you need to use System.Data.Odbc

Also, make sure your data source is installed correctly. It will make a difference.

Until you try these changes, it will be difficult for you to provide you much more help. Let us know what you have come up with, and we will be happy to help if you need it.

0
source

All Articles