I'm trying to a open a connection to a local database and no matter what I try it just won't work (I'm pretty new to SQL Server and VB for that matter) I tried using 'sqlconnection' with different connection strings but none of them worked or gave me any useful error messages so I tried using 'OleDBConnection' instead, this hasn't worked either and it gives me this error instead:
"A first chance exception of type 'System.NullReferenceException' occurred in MyProgram.exe"

Here is the code I am currently trying:

Private Sub MemDetailsSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ConnectionString = ("Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source = Database.sdf")

        DataConnect = New OleDbConnection(ConnectionString)

        Try

            DataAdapter.SelectCommand.Connection = DataConnect
            DataConnect.Open()
            MsgBox("Connection to database successful!")

        Catch ex As Exception

            MsgBox("Connection to database failed!")

        End Try
    End Sub
End Class

It's probably something simple as I am quite new to this and I'd appreciate if someone could point out what I'm doing wrong, thanks.

For Sql Compact database use classes from System.Data.SqlServerCe namespace.

Take a look at sample,

Dim cn As New System.Data.SqlServerCe.SqlCeConnection("Data Source=c:\Database1.sdf")
cn.Open()
Dim cmd As New System.Data.SqlServerCe.SqlCeCommand("create table test (tno int)", cn)
cmd.ExecuteNonQuery()
cn.Close()

PS: Add a reference of System.Data.SqlServerCe.DLL

Worked perfectly! Thanks :)

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.