more on can't get connection to open

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a full discourse in a recent post. But the
problem starts at the beginning. I start the program
with sub main. I put in msgboxes right at the beginning
to see where I stood before the error. When I run the
application, the message boxes before the cn.open
statement are not popped up on the screen. The program
goes directly to the connection error.

An unhandled exception of
type 'System.Data.OleDb.OleDbException' occurred in
system.data.dll

And the first three words of the statement

Dim cn As New OleDbConnection(gstrConn) are green.

What is going on? I'm misunderstanding something
fundamental. Please help.

dennist


Imports System.io

Imports System.Data
Imports System.Data.OleDb


Module modPrimary
Friend gstrConn As String
= "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" 'H:\HasbaraNET\ado.net
tests\adonetTest\HasbaraSample.mdb;"
Public s As String
Public gSelectedFile As String
Public gForm As Form
Public gsTopicText As String
Public gID As Integer
Public gsPath As String



Public Sub Main()
MsgBox("sub main opened")
Dim strPath As String
Dim strSubstring As String
Dim intIndexof As Integer
Dim cls1 As New Class1

strPath =
System.Reflection.Assembly.GetExecutingAssembly.Location
intIndexof = strPath.IndexOf("bin")
strSubstring = strPath.Substring(0, intIndexof)
gsPath = strSubstring
ChDir(strSubstring)
gstrConn = gstrConn & "HasbaraSample.mdb;"
SetGlobals()

MsgBox(gstrConn)
Dim cn As New OleDbConnection(gstrConn)
cn.Open()
MsgBox("sub main opened")

Application.Run(New frmTopicFromStart)
End Sub

Public Sub SetGlobals()
gSelectedFile = ""
End Sub


End Module
 
The first thing I'd look at is the ConnectionSTring. You have a ' after the
enclosing quotes, not sure if that's just a typo from the post, but if not,
that's going to give you some grief. You don't need the double quote
preceeding it either.

As an aside, you might want to wrap the open statement in a Try Catch block
b/c that can throw an exception. To check if it's opened, you can use a
Debug.Assert(cn.State = ConnectionState.Open)

Anyway, modify the connection string and verify it's correct before trying
to open it, let's see if that doesn't fix it.

For future reference, you may find this link helpful...
http://weblogs.asp.net/rosherove/posts/4748.aspx

HTH,

Bill
 
My problem was embarassingly simple. I forgot to put the
database in the project folder. Grin. After I did that
the problem disappeared.

dennist
 
Dennis,
A big difference in VB.NET (and other .NET languages) is that the Dim
statement is now "executable" code. This means if you make a mistake
building the ConnectionString, you can get an unhandled exception (as you
saw). What I do is delay instantiation and filling in the CS until I get
inside the Try/Catch block.

hth

--
____________________________________
Bill Vaughn
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Back
Top