Groups | Blog | Home
all groups > dotnet general > december 2005 >

dotnet general : HLP: Opening Access DB issues



Mr. B
12/6/2005 8:38:29 PM
VB.net 2003; MS Access db; Netframe 1.1

I wrote an Windows app for my last place of employment that opened and
modified a couple of MS Access db files. I did most of my programming at home
on Drive "C". And I used the Drag/Drop Data Items to create the Connection,
DataSet and DataAdapters.

The Access files were located on drives "P" and "S" at work, so I had VB.net
installed on my work PC so I could change the drives in the Connection Icons.

Now I no longer work at that place and they want me to make some changes. Of
course I can't as I don't have a drive P or S on my system. So I'm attempting
to get rid of the Data Icons and do the connection, Data set and adapters with
Code. And of course I'm getting all kinds of errors.

So I'm doing a Test app to find/fix my items. Right off the bat I can't even
establish a proper connection to a DB. The following is that code I'm using.

I get an ERROR at the FILL line (unhandled exception where "Value cannot be
null". But the access file opens just fine if I use the DATA Icons.

And... If I change the Fill line to:
daShenData.FillSchema(dsShenData, SchemaType.Source, "SHENHrsTable")
.... the error then says "The ConnectionString property has not been
initialized".

************** CODE ***********************************************
Imports System.IO
Imports System.Data.OleDb

' Connection String1 to SHENTSdata.mdb
Dim connString1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=TSdbPath & SHENTSdata.mdb"

Dim OleDbConnection1 As OleDbConnection = New OleDbConnection

' Data Adapter 1
Dim strSelect1 As String
strSelect1 = "SELECT Bill_Code, Dispersmts, Fri_Hrs, Job_Descrn,
Job_Number, Mileage," & _
"Mon_Hrs, Period_End_Date, PO_Order, Sat_Hrs, Sun_Hrs,
Thr_Hrs, Tue_Hrs," & _
"Wed_Hrs, Work_Descrn FROM SHENHrsTable"
Dim daShenData As New OleDbDataAdapter(strSelect1, OleDbConnection1)

' Dataset 1
Dim dsShenData As New DataSet

' Fill Dataset 1
daShenData.Fill(dsShenData.Tables("SHENHrsTable"))
'''daShenData.FillSchema(dsShenData, SchemaType.Source, "SHENHrsTable")
********************************************************************

QUESTION #1:
How do I fix the No Null value issue (why is it doing this anyways)?

QUESTION #2:
When I get this to work, where is the best place to place this code in my
application? In the Form Load? Between Form Load and "Windows Form Designer
generated code"? In a Module? In a Public Sub? This is because I open and
edit the Data bases elsewhere so I don't really know how to make it more
'globally' available.

QUESTION #3:
(a bit off topic) Or should I forget the whole thing... stick with my DATA
Icons... and use the SUBST command to create Virtual Drives and Fake it that
way?

Thanks muchly

Paul Clement
12/7/2005 9:10:56 AM
[quoted text, click to view]

¤ VB.net 2003; MS Access db; Netframe 1.1
¤
¤ I wrote an Windows app for my last place of employment that opened and
¤ modified a couple of MS Access db files. I did most of my programming at home
¤ on Drive "C". And I used the Drag/Drop Data Items to create the Connection,
¤ DataSet and DataAdapters.
¤
¤ The Access files were located on drives "P" and "S" at work, so I had VB.net
¤ installed on my work PC so I could change the drives in the Connection Icons.
¤
¤ Now I no longer work at that place and they want me to make some changes. Of
¤ course I can't as I don't have a drive P or S on my system. So I'm attempting
¤ to get rid of the Data Icons and do the connection, Data set and adapters with
¤ Code. And of course I'm getting all kinds of errors.
¤
¤ So I'm doing a Test app to find/fix my items. Right off the bat I can't even
¤ establish a proper connection to a DB. The following is that code I'm using.
¤
¤ I get an ERROR at the FILL line (unhandled exception where "Value cannot be
¤ null". But the access file opens just fine if I use the DATA Icons.
¤
¤ And... If I change the Fill line to:
¤ daShenData.FillSchema(dsShenData, SchemaType.Source, "SHENHrsTable")
¤ ... the error then says "The ConnectionString property has not been
¤ initialized".
¤
¤ ************** CODE ***********************************************
¤ Imports System.IO
¤ Imports System.Data.OleDb
¤
¤ ' Connection String1 to SHENTSdata.mdb
¤ Dim connString1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
¤ Source=TSdbPath & SHENTSdata.mdb"
¤
¤ Dim OleDbConnection1 As OleDbConnection = New OleDbConnection
¤
¤ ' Data Adapter 1
¤ Dim strSelect1 As String
¤ strSelect1 = "SELECT Bill_Code, Dispersmts, Fri_Hrs, Job_Descrn,
¤ Job_Number, Mileage," & _
¤ "Mon_Hrs, Period_End_Date, PO_Order, Sat_Hrs, Sun_Hrs,
¤ Thr_Hrs, Tue_Hrs," & _
¤ "Wed_Hrs, Work_Descrn FROM SHENHrsTable"
¤ Dim daShenData As New OleDbDataAdapter(strSelect1, OleDbConnection1)
¤
¤ ' Dataset 1
¤ Dim dsShenData As New DataSet
¤
¤ ' Fill Dataset 1
¤ daShenData.Fill(dsShenData.Tables("SHENHrsTable"))
¤ '''daShenData.FillSchema(dsShenData, SchemaType.Source, "SHENHrsTable")
¤ ********************************************************************
¤
¤ QUESTION #1:
¤ How do I fix the No Null value issue (why is it doing this anyways)?
¤
¤ QUESTION #2:
¤ When I get this to work, where is the best place to place this code in my
¤ application? In the Form Load? Between Form Load and "Windows Form Designer
¤ generated code"? In a Module? In a Public Sub? This is because I open and
¤ edit the Data bases elsewhere so I don't really know how to make it more
¤ 'globally' available.
¤
¤ QUESTION #3:
¤ (a bit off topic) Or should I forget the whole thing... stick with my DATA
¤ Icons... and use the SUBST command to create Virtual Drives and Fake it that
¤ way?
¤

Where is the line of code where you Open the OleDbConnection1 object?

Typically you open and close connections as you need them instead of maintaining persistent
connections. In this instance you probably want to add a Sub or Function to your Form and call it
from your Form Load event.

You can use the data connection objects (DATA Icons you called them) you created with the wizard,
but you need to change the connection string property at runtime (either in your Sub Main or Form
Load event).


Paul
~~~~
Andrew Morton
12/7/2005 1:12:20 PM
[quoted text, click to view]

Shouldn't that be
Dim connString1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& Path.Combine(TSdbPath, SHENTSdata.mdb)

....and where have you defined TSdbPath?


If I'm accessing a resource which is in a different location between the
development and deployment environments, I keep separate web.config files
for each environment and store the appropriate string in each.

So, in web.config I might have:-
<configuration>
<appSettings>
<add key="sqlConnectionString" value =
"Server=WHERE\EVER;Database=db;Trusted_Connection=true" />
</appSettings>

etc, and a different value in the other web.config.

and to retrieve the value:-
Private sqlConnectionString As String =
System.Configuration.ConfigurationSettings.AppSettings("sqlConnectionString")

HTH

Andrew

Stephen Howe
12/7/2005 1:32:02 PM
[quoted text, click to view]

What stops you from right-clicking on "My Network Places" and mapping an
appropriate pathed subdirectory on your hard disk as P or S drive?

[quoted text, click to view]

SUBST will effectively do the same thing

Stephen Howe

Brendan Reynolds
12/7/2005 2:21:21 PM
In addition to the other points that have been made, while you are declaring
the connString1 variable and assigning a value to it, nowhere in the code
that you have posted do you actually use it. It is not passed to the
connectionString argument when instantiating the OleDbConnection object or
assigned to the ConnectionString property of the object.

--
Brendan Reynolds

[quoted text, click to view]

Mr. B
12/7/2005 6:13:01 PM
With Deft Fingers, Paul Clement <UseAdddressAtEndofMessage@swspectrum.com>
[quoted text, click to view]


[quoted text, click to view]

So far it all has been in the Form Load. Just as I posted it.

[quoted text, click to view]

Okay. Using the 'Data Icons' I pretty well didn't have to do that as such.

[quoted text, click to view]

Thanks Paul!

Mr. B
12/7/2005 6:19:34 PM
[quoted text, click to view]

Well... good point. And the answer is I don't know. First off, I never got
any erry at that point, so I guess (to me) that it was okay.

But I just tried that and my "daShenData.FillSchema" line gives an error.

[quoted text, click to view]

It was a typical DIM statement after the "Inherits System.Windows.Forms.Form".
I just didn't think that was relevant (mind you I can see that you guys might
have thought that I missed that <grin>).

[quoted text, click to view]

Thanks Andrew... that's probably how I'll go.

Regards,

Mr. B
12/7/2005 6:21:52 PM
With Deft Fingers, "Stephen Howe" <stephenPOINThoweATtns-globalPOINTcom>
[quoted text, click to view]

Well you know... I didn't even think about trying that. For the simple reason
that I'm now working ot of my Home (SOHO). And thus I don't have any
'network'. So while I am very familiar with that method, I just didn't even
think about it for Home use. But for sure, a much better way of doing it (I
was raised up on DOS <grin>).

Thanks!

Regards,

Mr. B
12/7/2005 6:23:55 PM
With Deft Fingers, "Brendan Reynolds" <brenreyn@discussions.microsoft.com>
[quoted text, click to view]

Okay Brendan... you kind of lost me here on what you said :(

What do you mean that it is not pass to the ConnectionString argument?

Regards,

Stephen Howe
12/7/2005 6:46:47 PM
[quoted text, click to view]

Have a look at the code you posted.

See where you initialise connString1?
Now where is connString1 used in OleDbConnection1 below? Don't see it.

Stephen Howe


Mr. B
12/7/2005 6:53:49 PM
[quoted text, click to view]

Ah... hehe... do you happen to have the code to do this in a Windows app?
I've never done a Web app. (sigh). Menwhile I'm searching the Web for the
method!

Regards,

Stephen Howe
12/7/2005 6:55:20 PM
[quoted text, click to view]

You will get better answers from people if the information you first post is
_complete_, nothing missed out, all variables mentioned with declaration,
all intervening lines etc. Miss any lines of code out and the responses back
will on those very lines missed out (could they be the cause of your
problems?).

Stephen Howe


Brendan Reynolds
12/7/2005 9:36:45 PM
The OleDbConnection class has two constructors. One takes no arguments, the
second takes a connectionString argument. The OleDbConnection class also has
a ConnectionString property. In order to tell the object what connection
string you want it to use, you need to either use the second constructor and
pass the connection string in the connectionString argument ...

Dim connString1 As String = "a valid connection string goes here"
Dim OleDbConnection1 As New OleDbConnection(connString1)

.... or, if you use the constructor without arguments, assign the connection
string to the ConnectionString property ...

Dim connString1 As String = "a valid connection string goes here"
Dim OleDbConnection1 As New OleDbConnection
OleDbConnection1.ConnectionString = connString1

Here's a link to the on-line documentation on the OleDbConnection class ...
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbConnectionClassTopic.asp

--
Brendan Reynolds

[quoted text, click to view]

Mr. B
12/8/2005 5:02:09 AM
With Deft Fingers, "Brendan Reynolds" <brenreyn@discussions.microsoft.com>
[quoted text, click to view]

Thanks Brendan... but you know... I ended up and revised my code (using an
example of what I found on the Internet which simplified the lines of code).
The 'connection' part is just like you and Stepen said.

Interestingly enough, I STILL had an error at the DataSet Fill part... So I
played around with things for quite a while and then I 'accidently' discovered
the Problem (maybe you can explain this one).

Originally I had the following:

Dim dbPath As String = "C:\SHEN-EDL\Administration\Data\"

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=dbPath & SHENTSdata.mdb"


What I discovered, was for what ever reason, my dbPath String did NOT work in
the Connection String. So I added the full path in the String...

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\SHEN-EDL\Administration\Data\SHENTSdata.mdb"

POOF! Success! So no more dbPath strings (unless you can see the fix).

Go figure! You learn something every day (sometimes the hard way and many
hours later).

Regards,

Paul Clement
12/8/2005 8:19:44 AM
[quoted text, click to view]

¤ With Deft Fingers, Paul Clement <UseAdddressAtEndofMessage@swspectrum.com>
[quoted text, click to view]
¤
¤
¤ >Where is the line of code where you Open the OleDbConnection1 object?
¤
¤ So far it all has been in the Form Load. Just as I posted it.

I looked again and didn't see something that looks like the following:

OleDbConnection1.Open(connString1)

The bottom line is that according to the code you posted the connection is never opened.


Paul
~~~~
Andrew Morton
12/8/2005 9:13:01 AM
[quoted text, click to view]

The problem is that it doesn't know that the dbPath *inside* the quotes is
meant to refer to a variable, so you have to close the quotes, & the
variable in and then open the quotes again, like this:-

Dim dbPath As String = "C:\SHEN-EDL\Administration\Data\"

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
dbPath & "SHENTSdata.mdb"

Although for combining paths, it's better to use Path.Combine as it takes
care of adding in the path separator for you:-

Dim dbPath As String = "C:\SHEN-EDL\Administration\Data\"

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
Path.Combine(dbPath, "SHENTSdata.mdb")

(watch out for line
wrap)

Andrew

Mr. B
12/8/2005 6:32:19 PM
[quoted text, click to view]

Yeah... kinda thought that!

[quoted text, click to view]

Thanks Andrew... Path.Combine is a New one for me (so much still to learn).
Very appreciated!

I was going to try to have one variable (path and mdb file) and throw that
into the Conn string.

Regards,

AddThis Social Bookmark Button