mwilson
mwilson

Reputation: 12980

'C:\documents\TheHistoryDB.accdb' is not a valid path

Trying to write some history to an access datebase but I keep getting an error stating that the path is invalid. I'm using a connection string and I'm getting it from the wizard itself. Copy and Paste. Can anyone help me out?

Thanks

Imports System.IO
Imports System.Data.OleDb
Public Class theControls

'The History Database Connection String
Dim theHistoryDatabaseConn As New OleDbConnection

Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles theAddressBar.KeyDown
    'Navigate to Webpage stated in theAddressBar
    If e.KeyValue = Keys.Enter Then
        theBrowser.Navigate(theAddressBar.Text)
        e.SuppressKeyPress = True
    End If
End Sub

Private Sub goForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goForward.Click
    theBrowser.GoForward()
End Sub

Private Sub goBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goBack.Click
    theBrowser.GoBack()
End Sub

Private Sub theBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles theBrowser.DocumentCompleted

    'Set Tab Text to current web page
    Form1.TabControl1.SelectedTab.Text = theBrowser.Url.Host.ToString

    'The History

    theHistoryDatabaseConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\bin\Debug\TheHistoryDB.accdb"


    Dim theCommand As OleDbCommand = New OleDbCommand("INSERT INTO TheHistory ([Site]) VALUES (theBrowser.URL.Host)", theHistoryDatabaseConn)

    theCommand.Parameters.Add("@Site", OleDbType.Char, 255).Value = theBrowser.Url.Host.ToString

    Try
        theHistoryDatabaseConn.Open()
        theCommand.ExecuteNonQuery()

    Catch ex As Exception
        Throw ex
    Finally
        theHistoryDatabaseConn.Close()

    End Try
    theHistoryDatabaseConn.Close()
End Sub

Private Sub theBrowser_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles theBrowser.ProgressChanged

    'Status Bar Text
    Label1.Text = theBrowser.StatusText.ToString
End Sub
End Class

Upvotes: 1

Views: 1596

Answers (2)

matzone
matzone

Reputation: 5719

If you have database in the same folder with app, better use startuppath

theHistoryDatabaseConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\TheHistoryDB.accdb"

Upvotes: 0

mason
mason

Reputation: 32728

The title of your question says "C:\documents\TheHistoryDB.accdb" but your code is showing "|DataDirectory|\bin\Debug\TheHistoryDB.accdb". Are you sure you have your path correct?

Normally the folder used for documents would be something like "C:\Users\myusername\Documents". Not saying C:\documents doesn't exist on your computer, but just commenting that's an odd place for it.

Dim theCommand As OleDbCommand = New OleDbCommand("INSERT INTO TheHistory ([Site]) VALUES (theBrowser.URL.Host.ToString)", theHistoryDatabaseConn)

This is unrelated, but it doesn't look right to me. It looks like you're trying to access an object, but you're inside a string. I'm a C# guy, not VB. It just looks fishy to me. Also, why are you catching the Exception and immediately re-throwing it? Doesn't it kind of defeat the purpose?

Upvotes: 1

Related Questions