473,575 Members | 3,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switching ConnectionStrin g at runtime

Hi folks. I'm trying to change the location of the Access mdb file I
connect to using OleDb and am having trouble. I'm using Visual C# 2005
and the default values I use for the ConnectionStrin g are those
generated by the Server Explorer. I can connect just fine using the
default values.

But when I comment out strDefaultConne ctionFile and uncomment the
dbName variable I get:
'OleDbException : Could not use "; file already in use.' During testing
I select the exact same path and file that are used in the default, and
I have confirmed this by copying both 'runtime' strings to a text file
and comparing them.

So the two strings are identical. But the 2nd fails. Of course, I
close the debugger when switching between strings and have confirmed
that there are no locks on the database. And access permissions are
the same for the mdb in both cases. I am baffled!

Code fragments are below. Any help would be appreciated.

//Three strings defined earlier in the code...
strDefaultConne ctionPre =
@"Provider=Micr osoft.Jet.OLEDB .4.0;Password=" """;User ID=Admin;Data
Source=";

strDefaultConne ctionFile = @"C:\Program Files\HID\Confi g
Card Production Utility\ConfigC ard.mdb";

strDefaultConne ctionPost = @";Mode=ReadWri te|Share Deny
None;Extended Properties="""" ;Jet OLEDB:System database="""";J et
OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";J et
OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global
Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1; Jet OLEDB:New
Database Password="""";J et OLEDB:Create System Database=False; Jet
OLEDB:Encrypt Database=False; Jet OLEDB:Don't Copy Locale on
Compact=False;J et OLEDB:Compact Without Replica Repair=False;Je t
OLEDB:SFP=False ";

//Code fragment of a method called to define the data
source at runtime...

if (openFileDialog 1.ShowDialog() == DialogResult.OK )
{
if ((testStream = openFileDialog1 .OpenFile()) == null)
{
testStream.Clos e();
return false;
}
//Install the new path and test it.
dbName = openFileDialog1 .FileName;
oleDbConnection 1.ConnectionStr ing =
strDefaultConne ctionPre +
// @dbName +
strDefaultConne ctionFile +

strDefaultConne ctionPost;
if (!TestConnectio n())
{
return false;
}
}
else
return false;

Feb 13 '06 #1
1 1925
In case any one is interested, I found the error.

You will note I tested the result of the users file selection by trying
to open the selected file. I neglected to close the file stream on the
success branch. That resulted in the file being open when I later
tried to connect to the database. Once I closed the filestream
everything worked fine. Thanks for the help...

Feb 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2054
by: Jeremy Holt | last post by:
Hi, In a windows.forms application I would BeginInvoke a delegate on the UI thread to collect data from a database. When the call returns to the AsyncCallback, if the Control.InvokeRequired = True, I would then have the Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New Object() {ar}). How would one achieve the same...
2
1500
by: AllenL | last post by:
Ever since I've been using objects with VB I've instantiated the business object from the form; the business object then creates/destroys data access layer objects as needed. The business object also maintains state for the form variables. The form unload / closing event destroys the business object. For a small set of specialized forms in...
1
1560
by: A.Neves | last post by:
Hi, I'd like to have my ConnectionString retrieved from the same place in RunTime or in DesignTime (in VS2005). Is It possible? How? Any help would be very much appreciated. Thanks.
7
1570
by: hung tran | last post by:
Hi I use the class Grouper (it's an extended WinForm GroupBox ) for all my groupboxes, but now the customer wants to be able to switch between Grouper and GroupBox at runtime, how can I do that ? My idea is making my own class like MyGroupBox and in the designer I will call something like this.groupBox2 = new MyGroupBox ();, the class...
4
12487
by: wilson | last post by:
Hi. I'm new in VB.NET. I would like to ask a question. How to change ConnectionString for DataSet at runtime? Because I want to change the dBase data file directory according to the program installed directory of user choice. The DataSet is created using wizard at design-time. Thanks for your help.
1
5726
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access database - containing a list of addresses. Sequence of events is = (1) Excel template opens in its default XXX.xls filename. (2) Code runs to save...
3
7228
by: jobs | last post by:
I've got a gridview that does not have a datasourceid assigned in the markup. I'd like to switch between two datasources in the codebehind. when I do switch, I first reset the the gridviewx.datasource = nothing and gridviewx.datasourceid = nothing before setting it to gridviewx.datasource = newdatasoruceid and then rebinding. All...
1
1784
by: SamSpide | last post by:
Hi all, I have a moderate-side 'Windows Form Application' (C++) project, with several forms. For some reason switching between code & designer views (right-click ;view code' or 'view designer') is very slow. How slow? It can take more than 10 seconds for a truely moderate-size form.
3
7967
by: Michael Moreno | last post by:
Hi, We have an app that is made of many exes (WinForms and Win32 app). All those exes share and read their DB connection string from the registry. This guarantees that all the exes use the same DB. With .Net the connection string is in the app.config file as soon as you use the design time functionality to bind data to grids as an...
0
7845
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7775
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7868
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8143
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6515
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3778
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3797
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1107
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.