这是我的代码。我似乎无法弄清楚是什么导致了这个错误。错误本身指向Do While myReader.read
代码行,但我不确定这是导致问题的原因。
以下是更详细的错误消息:
executereader:连接属性尚未初始化。 at system.data.oledb.oledbcommand.validateconnection(string method)at system.data.oledb.oledbcommand.validateconnectionandtransaction(string method)
代码:
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
答案 0 :(得分:1)
您已经创建了连接,然后创建了命令,但是您从未将连接分配给命令,这就是您收到错误的原因。您可以将连接分配给OleDbCommand
,如下所示:
sqlCmd.Connection = myDB
或者您可以在OleDbCommand
构造函数中指定,如下所示:
sqlCmd = New OleDbCommand("exec qry_OrderHistory", myDB)
然后您可以打开连接并执行命令。
答案 1 :(得分:0)
您必须在使用之前初始化您的Connective实例myConnection