[RESOLVED] Replace |DataDirectory| or replace with own string.
Hi
I would like to allow my users to decide where they want their data to reside. I have created a data source which is referenced by crystal reports. I would like to replace the |DataDirectory| to point to a path the user selects.
or if possible substitute it with vb function.
Below is the line in xml that point to my db. I would like to allow it to call a vb function that will return my db path
I am not sure how to replace the connection string by using a vb function in xml.
Code:
<Connection ConnectionStringObject="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MY_DB.mdb;Persist Security Info=True;Jet OLEDB:Database Password=pass" IsAppSettingsProperty="false" Modifier="Assembly" Name="MY_DB.mdb" Provider="System.Data.OleDb" />
I would like to replace |DataDirectory| with the result of a function called MyDBPath(), can anyone assist.
Re: Replace |DataDirectory| or replace with own string.
String.Replace is always a good bet.
Re: Replace |DataDirectory| or replace with own string.
What I need to do is to insert a function that gets the path into the xml code.
In vb I would use:
Code:
ConnString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyDBPath() & "\MY_DB.mdb;Persist Security Info=True;Jet OLEDB:Database Password=pass"
How do I insert that same function into the xml code below?
Code:
<Connection ConnectionStringObject="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MY_DB.mdb;Persist Security Info=True;Jet OLEDB:Database Password=pass" IsAppSettingsProperty="false" Modifier="Assembly" Name="MY_DB.mdb" Provider="System.Data.OleDb" />
Re: Replace |DataDirectory| or replace with own string.
Are you reading from an XML file, is this WPF? You'll have to be a little clearer.
Re: Replace |DataDirectory| or replace with own string.
Sorry, I am new at it, and will try to explain myself better.
I have used the wizard provided with vs 2008 to connect to a data source. VS automatically created an *.xsd file for the data source. Inside this xsd file is the connection string to my databas. In an attempt to allow the user to place this dataset where he wishes to and still be able to connect to it, I would like to use an existing vb function to provide the database path to the xml code in the *.xsd file.
Hope this makes it a little clearer.
Re: Replace |DataDirectory| or replace with own string.
You should keep using |DataDirectory| in your connection string and then simply set the data directory for your AppDomain at startup, e.g.
vb.net Code:
AppDomain.CurrentDomain.SetData("DataDirectory", My.Settings.DataDirectory)
You would pass the path chosen by the user as the second argument. You can store it in your application settings, as I've demonstrated, or somewhere else.
Re: Replace |DataDirectory| or replace with own string.
Or if you really want to do it on-the-fly every time, you can use a FolderBrowserDialog and pass the SelectedPath property as the second parameter of the method shown by jmcilhinney.
Re: Replace |DataDirectory| or replace with own string.
Thanks jmcilhinney, this realy helped.