Click here to Skip to main content
15,896,448 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i search record for CODE

It says the connectionstring property has not been initialized

Please correct me

What I have tried:

VB
Private Sub txtsearch_TextChanged(sender As Object, e As EventArgs) Handles txtsearch.TextChanged

        ConnD()
        Try

        
        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter
        da = New OleDbDataAdapter("Select * from SONGS where CODE like ?", con)
        da.SelectCommand.Parameters.AddWithValue("Code", "%" & txtsearch.Text & "%")
        da.Fill(dt)
            conn.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Posted
Updated 4-Feb-20 20:10pm
v5

The error message tells you that SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Docs[^] has not been initialized.
The problem most probably lies into the ConnD() method, the content of which you forgot to show. Moreover you seem to have two variables for the connection: con and conn; which is the proper one? It would also be useful to know where the conn or con variable has been declared and initialized.
 
Share this answer
 
Note, that below statement is incorrect:
VB.NET
da.SelectCommand.Parameters.AddWithValue("Code", "%" & txtsearch.Text & "%")


MS Access database engine does not recognize [%] character. See:
SELECT statement (Microsoft Access SQL) | Microsoft Docs[^]
Like operator (Microsoft Access SQL) | Microsoft Docs[^]

If you would like to find a value which contains specific text, use: *specific_text*
If you would like to find a value which starts with specific text, use: specific_text*
If you would like to find a value which ends with specific text, use: *specific_text
 
Share this answer
 
Comments
Member 14621280 5-Feb-20 2:59am    
where code would be the table column name
VB
da = New OleDbDataAdapter("Select * from SONGS where CODE like ?", con)
da.SelectCommand.Parameters.AddWithValue("Code", "%" & txtsearch.Text & "%")

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
v2
Comments
Richard Deeming 6-Feb-20 10:33am    
That code is already using parameters, and is not vulnerable to SQL Injection.
Patrice T 6-Feb-20 10:39am    
oops my bad.
Member 14621280 11-Feb-20 1:25am    
So.... another suggestions please

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900