Vb.net – Error “ExecuteReader: connection property has not been initialized.”

asp.netoledbvb.net

Here's my code. I can't seem to figure out what's causing this error. The error itself pointed to the Do While myReader.read line of code, but I'm not sure that's what's causing the problem.

Here is a more detailed error message:

executereader: connection property has not been initialized. at system.data.oledb.oledbcommand.validateconnection(string method) at system.data.oledb.oledbcommand.validateconnectionandtransaction(string method)

Code:

Imports System.Data.OleDb
Imports System.Data

Partial Class Customer_6_OrderHistory
    Inherits System.Web.UI.Page

    Private myDB As OleDbConnection
    Private sqlCmd As OleDbCommand
    Private myReader As OleDbDataReader
    Private myConnection As String = ConfigurationManager.ConnectionStrings("ConnString10").ToString
    Private myDataFile As String = ConfigurationSettings.AppSettings("DBFile")

    Private LegoNameList As New ArrayList
    Private ShipmentDateList As New ArrayList
    Private CostList As New ArrayList
    Private NumberPurchasedList As New ArrayList
    Private RecipientList As New ArrayList
    Private TotalCostList As New ArrayList

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        myDB = New OleDbConnection(myConnection)

        sqlCmd = New OleDbCommand("exec qry_OrderHistory")
        sqlCmd.Parameters.AddWithValue("@CustomerName", Session("myCart").GetCustomerName())

        Try
            myDB.Open()
            myReader = sqlCmd.ExecuteReader
            Do While myReader.Read
                LegoNameList.Add(myReader("Lego Name"))
                ShipmentDateList.Add(myReader("Date"))
                CostList.Add(myReader("Cost"))
                NumberPurchasedList.Add(myReader("Number Purchased"))
                RecipientList.Add(myReader("Recipient"))
                TotalCostList.Add(myReader("Total Cost"))
            Loop
            myReader.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            myDB.Close()
        End Try

        Dim myDataTable As New DataTable
        myDataTable.Columns.Add("Lego Name")
        myDataTable.Columns.Add("Date")
        myDataTable.Columns.Add("Cost")
        myDataTable.Columns.Add("Number Purchased")
        myDataTable.Columns.Add("Recipient")
        myDataTable.Columns.Add("Total Cost")

        For i = 0 To LegoNameList.Count - 1
            Dim myRow As DataRow = myDataTable.NewRow
            myRow.Item("Lego Name") = LegoNameList.Item(i)
            myRow.Item("Date") = FormatDateTime(ShipmentDateList.Item(i), DateFormat.ShortDate)
            myRow.Item("Cost") = FormatCurrency(CostList.Item(i), 2, TriState.True, TriState.False, TriState.True)
            myRow.Item("Number Purchased") = NumberPurchasedList.Item(i)
            myRow.Item("Recipient") = RecipientList.Item(i)
            myRow.Item("Total Cost") = FormatCurrency(TotalCostList.Item(i), 2, TriState.True, TriState.False, TriState.True)
            myDataTable.Rows.Add(myRow)
        Next
        gvwOrderHistory.DataSource = myDataTable
        gvwOrderHistory.DataBind()
    End Sub
End Class

Best Answer

You have created your connection, and then you create your command, but you never assign the connection to your command, which is why you're getting the error. You can either assign the connection to your OleDbCommand, like this:

sqlCmd.Connection = myDB

Or you can specify in the OleDbCommand constructor, like this:

sqlCmd = New OleDbCommand("exec qry_OrderHistory", myDB)

Then you can open the connection and execute the command.

Related Topic