1.11M Members

Need help on ExecuteReader: Connection property has not been initialized =(

 
0
 

Hi guys i am a total beginner on VB.As mention on the tittle i would love a help on my problem.Here is my code

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class Form1

Dim conn As New System.Data.OleDb.OleDbConnection()


Private Sub btnSignUp_Click(sender As Object, e As EventArgs) Handles btnSignUp.Click
    Me.Hide()
    Form2.Show()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

Private Sub btnSignIn_Click(sender As Object, e As EventArgs) Handles btnSignIn.Click


    Dim sql As String = "SELECT * FROM table WHERE Username='" & UsernameTextBox.Text & "' AND Password = '" & PassTextBox.Text & "'"
    Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)


    Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()

    If sqlRead.Read() Then
        Form3.Show()
        Me.Hide()

    Else
        ' If user enter wrong username and password combination
        ' Throw an error message
        MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        'Clear all fields
        PassTextBox.Text = ""
        UsernameTextBox.Text = ""

        'Focus on Username field
        UsernameTextBox.Focus()
    End If

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ser\Desktop\test.accdb"
    If conn.State = ConnectionState.Closed Then
        MsgBox("OPEN")
    Else
        MsgBox("CLOSE")
    End If

End Sub

End Class

 
0
 

You have to open the connection before you read. Here's an example

lvwResults.Items.Clear()

Dim con As New OleDbConnection("Provider=SQLNCLI10;Server=.\SQLEXPRESS;Database=PUBS;Trusted_Connection=Yes;Connect Timeout=15;")
Dim cmd As New OleDbCommand("", con)

cmd.CommandText = "SELECT au_lname,au_fname,phone " _
                & "  FROM authors                 " _
                & " WHERE au_lname like ?         " _
                & "   AND phone    like ?         " _
                & " ORDER BY au_lname             "

cmd.Parameters.AddWithValue("@lastName", txtLastName.Text)
cmd.Parameters.AddWithValue("@phone   ", txtPhone.Text)

con.Open()
Dim rdr As OleDbDataReader = cmd.ExecuteReader()

Do While rdr.Read
    lvwResults.Items.Add(New ListViewItem({rdr("au_lname"), rdr("au_fname"), rdr("phone")}))
Loop

rdr.Close()
con.Close()
 
0
 

Hi,

Also you need to specify that your command will use the connection...

 
0
 

That was supplied as the second parameter in the declaration

Dim cmd As New OleDbCommand("", con)
 
0
 

hey ty very much reverend jim =)

Question Answered as of 8 Months Ago by Reverend Jim and G_Waddell
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article