Reputation: 11
Every time I tried to connect to the database it give me this error "The ConnectionString property has not been initialized"
How can i avoid this error and make it work?
Here are my codes:
Imports System.Data.OleDb
Public Class frmLoginPage
Dim con As New OleDbConnection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("Select*from tblPassword", con)
da.Fill(dt)
Dim newRow As DataRow = dt.NewRow
With newRow
If .Item("Username") = txtUsername.Text And .Item("Password") = txtPassword.Text Then frmMainMenu.ShowDialog() Else MsgBox("The username or password you entered was incorrect, please try again!", MsgBoxStyle.Critical, "Information")
End With
End Sub
Upvotes: 1
Views: 10882
Reputation: 121
You never assigned your connection string to the connection object, just like the error is saying.
Insert a line setting the connection string before con.open.
Con.connectionstring = connection Con.Open() Or better yet, change your using statement as follows
Dim Connection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=G:\VB Project\Library Catalog System\Library Catalog System\library.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Using Con As New SqlConnection(connection)
Upvotes: 1
Reputation: 706
This Problem Occurs when you donot consider making a new connection The solution is very simple, all you have to do is to make
Dim conString as string = "Your connection string here"
Dim con As new OleDbConnection
con.connectionSting= ConSting
con.open()
'now simply write the query you want to be executed
'At the end write
con.close()
this will solve your problem. if it doesn't solve your problem post reply I will try to solve it.
Upvotes: 1
Reputation: 7262
You have instantiated an OleDbConnection
object, but you haven't set the connectionstring
property so it doesn't know where it should connect to. You also haven't opened it. Your code should look something like:
Dim myConnection As OleDbConnection = New OleDbConnection()
myConnection.ConnectionString = myConnectionString
myConnection.Open()
' execute queries, etc
myConnection.Close()
Upvotes: 2