question

WilfredEghenedji-9495 avatar image
0 Votes"
WilfredEghenedji-9495 asked JiachenLiMFST-9349 commented

Search texts in access db and show result in a label

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


dotnet-visual-basic
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

but gives error:

Which complete error message do you get and in which line of code?
0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered JiachenLiMFST-9349 commented

Maybe rename New2 to New.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your response. However, after renaming to 'New', it says: **

Overload resolution failed because no 'New' is accessible.




0 Votes 0 ·
Viorel-1 avatar image Viorel-1 WilfredEghenedji-9495 ·

Try changing it to 'Public Sub New( )'.


1 Vote 1 ·

Thank you very much. This solved the problem.

0 Votes 0 ·
Show more comments