Distribute database on end user machine - vb.net

I have a little desktop application using a *.db3 database. When I run the application on other machine than mine, the database cannot be found: an absolute path is used.
Currently, in my App.Config file, I have
connectionStrings>
add name="MyProject.My.MySettings.MyProjectSQLiteConnectionString"
connectionString="data source="N:\Long path do my DB\MyDB.db3""
providerName="System.Data.SQLite" />
/connectionStrings>
I would like to replace the data source with something like this (unfortunately not working)
data source =|DataDirectory|\MyDB.db3
How can I enter the application directory (relative path) in my connection string?

Starting with Steve B's comment and later finding this article, I solved my problem doing the following:
On the user machine, the database is copied into the %AppData% directory.
The |DataDirectory| is dynamically changed between Debug and Release mode.
This allows not to change the connection string.
In the meantime, I opted for a MS Access *.accdb database, but here is my code anyway.
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
#If (Not Debug) Then
pathMyAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\MyApp"
'"DataDirectory" is used in the Connection String
AppDomain.CurrentDomain.SetData("DataDirectory", pathMyAppData)
'The application closes if no database is found
If Not File.Exists(pathMyAppData & "\MyDB.accdb) Then
MsgBox("Database not found. Program closes.", MsgBoxStyle.Critical, "Error")
Me.Close()
End If
#End If

Related

"The Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine." Exception is thrown while trying to run forms. eg purchases form

I've been trying to connect a Microsoft access database file to my Visual Basic application. When i try to run the application i get an error: "The Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine" i am using microsoft visual basic 2012.
Public Class purchases
Private Sub Purchases_fileBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Purchases_fileBindingNavigatorSaveItem.Click
Me.Validate()
Me.Purchases_fileBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.TNJ_Retail_Management_SystemDataSet)
End Sub
Private Sub purchases_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'TNJ_Retail_Management_SystemDataSet.Purchases_file' table. You can move, or remove it, as needed.
Me.Purchases_fileTableAdapter.Fill(Me.TNJ_Retail_Management_SystemDataSet.Purchases_file)
End Sub
End Class
Check link for screenshot of output. 1
Does anyone have any idea?
Was able to correct the error simply by downloading Microsoft Access Database Engine and installing it in my PC. I then opened my project, rebuild it and run. Gave expected results. Click here to open site to download the Microsoft Access Database Engine

Exe working only if started manually but I want it to start automatically

I have done a simple VB application with this code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim procName As String = Process.GetCurrentProcess().ProcessName
Dim processes As Process() = Process.GetProcessesByName(procName)
If processes.Length > 1 Then
Process.GetProcessesByName("keyinput")(0).Kill()
End If
End Sub
Public Sub type(ByVal int As Double, str As String)
For Each c As Char In str
SendKeys.Send(c)
System.Threading.Thread.Sleep(int * 1000)
Next
End Sub
Sub vai()
Dim line As String = ""
If File.Exists("trans.txt") Then
Using reader As New StreamReader("trans.txt")
Do While reader.Peek <> -1
line = reader.ReadLine()
type(0.155, line)
'SendKeys.Send(line)
SendKeys.Send("{ENTER}")
Loop
End Using
File.Delete("trans.txt")
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
vai()
End Sub
Basically the timer in it check if a file exists, read it and type the content simulating the keyboard.
I want this exe to start automatically when user login, it does it, apparently, I can see the form1 pop up but doesn't really works. Everyting is fine only if I run it manually by double-clicking the icon. Why and what can I do? Thanks
ps. i already tried to execute it with windows task manager, or putting a shortcut in the windows startup folder, or calling it from a cmd
EDIT:
when app starts automatically , process is running, but windows form is showing like this
Instead starting manually is showing like this:
I don't know this for a fact but I suspect that the issue is the fact that you are not specifying the location of the file. If you provide only the file name then it is assumed to be in the application's current directory. That current directory is often the folder that the EXE is in but it is not always and it can change. DO NOT rely on the current directory being any particular folder. ALWAYS specify the path of a file. If the file is in the program folder then specify that:
Dim filePath = Path.Combine(Application.StartupPath, "trans.txt")
If File.Exists(filePath) Then
Using reader As New StreamReader(filePath)
EDIT:
If you are running the application at startup by adding a shortcut to the user's Startup folder then, just like any other shortcut, you can set the working directory there. If you haven't set the then the current directory will not be the application folder and thus a file identified only by name will not be assumed to be in that folder.
If you are starting the app that way (which you should have told us in the question) then either set the working directory of the shortcut (which is years-old Windows functionality and nothing to do with VB.NET) or do as I already suggested and specify the full path when referring to the file in code. Better yet, do both. As I already said, DO NOT rely on the current directory being any particular folder, with this being a perfect example of why, but it still doesn't hurt to set the current directory anyway if you have the opportunity.
It was a Windows task scheduler fault, that for some reason didn't executed the exe correctly at logon. I've solved the issue by using Task Till Down and everything works fine now.

select files from setup location vb

I work on my VB application on visual studio 2012
I created a button, On clicking it, it plays a file on my PC ("D:\My Project\Sound_01.wav")
so the code will be like that:
**Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
My.Computer.Audio.Play("D:\My Project\Sound_01.wav")
End Sub**
My problem is that I need to make setup file for that app (using install shield 2015) and the file location will return error because the destination PC may not have the same location("D:\My Project\Sound_01.wav")
can anyone advise me how to do that?
add the file to your application path so you can use following:
My.Computer.Audio.Play(Application.StartupPath & "\Sound_01.wav")

vb.net 2008 multilingual string display adding resources err:MissingManifestResourcesException

Developing a multilingual application in VB.Net 2008, Im able to add resources to forms and create a multilingual forms depending on uiculture. On reading Msdn on creating the multilingual string values for messagebox contents, have added the .resource file to the project files path as specified. There is no error on compilation but throws the MissingManifestResourceException error
Dim rm As ResourceManager
rm = ResourceManager.CreateFileBasedResourceManager("strFormResources", ".", Nothing)
Dim ci As CultureInfo
ci = New CultureInfo("fr-FR")
MessageBox.Show(rm.GetString("sample1", ci))
Could not find any resources appropriate for the specified culture (or the neutral culture) on disk. baseName: strFormResources locationInfo: fileName: strFormResources.resources
There is strFormResources.resources and strFormResources.fr-FR.resources in Resources of the project.
I have searched for this error details but could not find a solution. What am i doing wrong or is there any other method for displaying multilingual strings in the messagebox.
Thanks in advance
The lblBrowsefoldertoputconvertedfiles is a hint perhaps. You are supposed to pass the string resource name, not the name of the directory that contains the resource.
To do it "properly", be sure to take advantage of the My.Resources feature. Proceed as follows:
Project + Add New Item, General, Resources File
Name it Resources.fr-FR.resx and click Add
The string resource editor automatically opens. Add the strings that you've got in your original string table, now using French as the language.
Compile.
Look in your project's bin\Debug folder and verify that you now see the satellite assembly. It should be stored in the fr-FR directory with the project.resources.dll name.
Test this by dropping a button on your form and writing this code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Threading.Thread.CurrentThread.CurrentUICulture = _
System.Globalization.CultureInfo.GetCultureInfo("fr-FR")
MessageBox.Show(My.Resources.String1)
End Sub

No Access to an mdb database file after copying the file

I have the following code where I make an copy of my database that I use. the code executes 100% but the problem I have is I'm not able to access my database afterward.I get a "Cannot start your application. The workgroup information file is missing or opened exclusively by another user." and so if I restart the application it all works fine again.
I'm certain the reason is because I can only access that database using a specific account name and password which is not account I'm logged in with.
What i want to try is to default that database mdb to the point where no user is accessing it, or to reassign the the only account name that can access it to that database mdb.
Any ideas, will be enormously appreciated? I've tried playing around with file security but had no luck.
Private Sub cmdBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBackup.Click
BackupDialogDB.DefaultExt = ".mdb"
BackupDialogDB.InitialDirectory = "c:\"
'SaveFileDialog1.ShowDialog()
If BackupDialogDB.ShowDialog() = Windows.Forms.DialogResult.OK Then
Try
Dim sDBFile As String = Application.StartupPath + "\VFMS_DB.mdb"
Dim sBackUpFile As String = BackupDialogDB.FileName
'First check the file u want to compact exists or not
If File.Exists(sDBFile) Then
If Not File.Exists(sBackUpFile) Then
File.Copy(sDBFile, sBackUpFile)
Else
File.Delete(sBackUpFile)
File.Copy(sDBFile, sBackUpFile)
End If
MessageBox.Show("The database was successfully backedup to: " + sBackUpFile , "Database Backedup", MessageBoxButtons.OK, MessageBoxIcon.Information)
sDBFile = ""
Else
MessageBox.Show("There is no database to backup. Please restore from a backup", "Database Backup Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
You should also copy and/or restore the workgroup information file.
Do you have write permission in the target directory? Access needs to create a .ldb file along side the .mdb.
Otherwise, you can try some of the stuff here:
http://www.xlinesoft.com/asprunnerpro/docs/error_the_microsoft_jet_database_engine_cannot%20open%20the%20file%20(unknown).htm
Was the Database open when you copied it? Access creates lock files, *.ldb in the same directory the database is in, you might want to check for that file before you copy the database.