So I put the database into the dubug folder in bin of my project, and in the data source configuration wizard i selected ms access and my file, then i copied my connection string, it was "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\bin\Debug\bookshop.mdb" and wrote some code for it to connect to it, to be exact, i used the code in this webpage: http://www.rollybueno.info/how-to-create-login-form-in-vb-net/ and when i debug it and enter the username and password, i getthe "failed to connect to database" that was made by the cod,i checked to see if i had missed some lines of code or just a symbol, i haven't, I searched so much and couldn't find a solution, can someone please help, thanks in advance

here is my code

`Public Class frmLogin

Private Sub btnSI_Click(sender As Object, e As EventArgs) Handles btnSI.Click
    Dim u As String = txtUN.Text
    Dim p As String = txtPW.Text

    If u = "admin" And p = "admin123" Then
        Me.Hide()
        frmSystemAdmin.Show()

    Else
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\bin\Debug\bookshop.mdb"

        Try

            Dim sql As String = "SELECT * FROM Login WHERE username='" & u & "' AND password = '" & p & "'"
            Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)

            sqlCom.Connection = conn
            conn.Open()

            Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()

            If sqlRead.Read() Then
                frmCashier.Show()
                Me.Hide()

            Else
                MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                p = ""
                u = ""

                txtUN.Focus()
            End If

        Catch ex As Exception
            MessageBox.Show("Failed to connect to Database..", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End If
End Sub

End Class`

Recommended Answers

All 10 Replies

It looks to me that in line 9 you want to connect if the passwords are wrong? Check your if--then--else statement

Check your database pasword..

@minimalist: Lines 5-7 is the code for the system admin, he will be taken to a different form, and that part of the code works right. But it's after that where the problem is, in this part the application will look in the database from the username and password field for a matchin username and password, and if it cannot connect, i gives the error message it is giving me.

@satti: It has no password.

I forgot to mention that when I put the database somewhere like the C drive for example or anything that doesn't say "|DataDirectory|" and then the connection string is: "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/bookshop.mdb", it works just fine.

I've tried searching alot for this and couldn't find a proper solution, i was hoping that someone here has encountered or dealt with such a problem?

And thanks for the replies :)

In the documentation for source it says:
Data Source = |DataDirectory|\Mydb.mdb
so if I look at your connection string you still have directory references in the path which is not allowed.Click Here

Breakpoint on Line 19 (Press F9) --> conn.Open() , then debug it, see if can open your database.

@minimalist: sorry ididn't get you, and the link you've given is broken.

@ryanjayson: just did, no it doesn't, it only minimizes the form.

I'm sure theres something wrong with the "|DataDirectory|" part, just can't figure out what :/

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\bin\Debug\bookshop.mdb" shoulds be like this:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\bookshop.mdb"

@minimalist: No Problem, thanks for the reply :)

I published the app once halfway through, just to check a few things, could this be why it doesn't connect to the database? through the data directory way?

@minimalist: it works :D woohhoooo

Thanks Alot, my assignment submission is tomorrow and you have just taken this huge monkey off my back :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.