0

I get this error:
the connectionstring property has not been initialized
when I execute a SELECT . This is the calling code:

Sub New()
        Dim con As New OleDbConnection
        If IO.File.Exists(Application.StartupPath & "\lab2015.accdb") Then
            If My.Settings.constr = "" Then
                con = New OleDbConnection(("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath() & "\lab2015.accdb;Jet OLEDB:Database Password=mak;"))
            Else
                If IO.File.Exists(My.Settings.constr) Then
                    con = New OleDbConnection(("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Settings.constr & ";Jet OLEDB:Database Password=mak;"))
                Else
                    If MessageBox.Show("هل تريد تحديد مسار قاعدة البيانات", "خطأ قاعدة البيانات غير موجودة", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign) = DialogResult.Yes Then

                        Database.Show()
                    Else
                        MessageBox.Show("سيتم استخدام قاعدة البيانات الحالية ", "تنويه", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
                        con = New OleDbConnection(("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath() & "\lab2015.accdb;Jet OLEDB:Database Password=mak;"))

                    End If
                End If


            End If
        Else
            MessageBox.Show("قاعدة البيانات غير موجودة", "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
            Exit Sub
        End If
    End Sub
    REM جلب البيانات بطريقة الاتصال المغلق
    Public Function selectdatatable(seltxt As String) As DataTable

        If con.State = ConnectionState.Open Then
            con.Close()
        End If
        Dim da As New OleDbDataAdapter(seltxt, con)
        Dim dt As New DataTable
        con.Open()
        da.Fill(dt)
        Return dt
        con.Close()
    End Function

I am attempting to connect to an Access database.

Michael Armes
  • 1,056
  • 2
  • 17
  • 31
  • 1
    You need to learn about `Scope`. Your connection is declared (`Dim`) in `Sub New`, so that is the only place it exists. Declare a form/class level connection *string* and then just declare, use and dispose of a connection every time you need one. Please see [Quick tutorial on Scope](http://stackoverflow.com/a/33249045/1070452) – Ňɏssa Pøngjǣrdenlarp Oct 20 '16 at 23:46
  • oh.yes it crossed my mind , thanks alot bro. – Makky Mohamad Oct 22 '16 at 16:40

0 Answers0