Am trying to code on searching a text in Access database and the result appear in label1. I have tried writing code as follows but gives error:
connectionstring property has not been initialized
Please help, resolve Thank you.
Imports System.Data.OleDb
Public Class Form1
Private connection As OleDbConnection = New OleDbConnection()
Private Sub New2()
InitializeComponent()
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=DictionaryDB.accdb; Persist Security Info=False;" 'Server connection --- Obtain access connection string from wwww.connectionstrigns.com
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Try
connection.Open()
checkConnection.Text = "Server Connection, Successful"
connection.Close()
Catch ex As Exception
'MessageBox.Show("Error, Server Not connected " & ex)
End Try
End Sub
Private Sub button126_Click_1(sender As Object, e As EventArgs) Handles button126.Click
connection.Open()
Dim command As OleDbCommand = New OleDbCommand()
command.Connection = connection
command.CommandText = "Select * from testing where WordID='" & txt_WordID2.Text & "'"
Dim reader As OleDbDataReader = command.ExecuteReader()
Dim count As Integer = 0
While reader.Read()
count = count + 1
End While
connection.Close()
If count = 1 Then
connection.Open()
command.Connection = connection
command.CommandText = "Select Meanings from testing where WordID='" & txt_WordID2.Text & "'"
Dim dr As OleDbDataReader = command.ExecuteReader()
While dr.Read()
lblMeaning2.Text = dr("Meanings").ToString()
End While
dr.Close()
connection.Close()
Else
panelWordNotInDictionary2.Visible = True
End If
End Sub
End Class