Data path changes after saving files with SaveFileDialog

  • Thread starter Microsoft News Server
  • Start date
M

Microsoft News Server

Using VB.NET

I have the path to my Access file stored in an App.config file:

<add key="constring" value="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=Data/METL.mdb;Persist Security Info=False"/>

I usually set the connection like this:

'* Get the appSettings constring value (set in the web.config)
strConstring =
System.Configuration.ConfigurationSettings.AppSettings("constring")

'* Connects to database
ocnConnection = New OleDb.OleDbConnection(strConstring)

After creating an Excel file and saving it to a folder other than the
application folder, my database connections always try to find the database
file in the folder the Excel file was saved to.

If dlgSaveFile.ShowDialog() = DialogResult.OK Then
Excel.DisplayAlerts = False
Excel.AlertBeforeOverwriting = False
Excel.ActiveWorkbook.SaveAs(dlgSaveFile.FileName)
Excel.ActiveWorkbook.Close()
Else
Excel.ActiveWorkbook.Close(False)
End If


Thanks,

Sacha
 
S

Sacha Korell

I found the solution. After saving, I needed to change the current directory
back to the executing assemblies directory:

Directory.SetCurrentDirectory(System.Reflection.Assembly.GetExecutingAssembly.Location.Substring(0,
System.Reflection.Assembly.GetExecutingAssembly.Location.LastIndexOf("\") +
1))

Thanks,

Sacha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top