473,387 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Invalid attempt to FieldCount when reader is closed

CW
I get this FieldCount error when I attempt to bind a datagrid with a
dataset, not a datareader object.

The code snippet is as belows:

'PopulateForm called in Page Load event
Private Sub PopulateForm(ByVal MyOrderDetails As MyChannel.OrderDetails)

Me.lblOrderID.Text = CStr(MyOrderDetails.OrderID)

Dim customer As MyChannel.CustomersDB = New MyChannel.CustomersDB()

Dim DR As SqlClient.SqlDataReader =
customer.GetCustomerList(CInt(Me.Page.User.Identit y.Name))

Me.DDLCustomer.DataSource = DR

Me.DDLCustomer.DataValueField = "CustomerID"

Me.DDLCustomer.DataTextField = "FullName"

Me.DDLCustomer.DataBind()

DR.Close()

Me.DDLCustomer.SelectedIndex = GetDDLItemIndexByValue(Me.DDLCustomer,
CStr(MyOrderDetails.CustomerID))

Me.lblOrderDate.Text = Format(MyOrderDetails.OrderDate, "d")

Me.tbCustomerPO.Text = MyOrderDetails.CustomerPO

Me.tbDelAddLine1.Text = MyOrderDetails.DelAddLine1

Me.tbDelAddLine2.Text = MyOrderDetails.DelAddLine2

Me.tbDelAddLine3.Text = MyOrderDetails.DelAddLine3

Me.tbDelAddLine4.Text = MyOrderDetails.DelAddLine4

Me.tbShipDate.Text = Format(MyOrderDetails.ShipDate, "d")

Me.tbShipVia.Text = MyOrderDetails.ShipVia

Me.tbComments.Text = MyOrderDetails.Comment

Me.tbFreight.Text = Format(MyOrderDetails.Freight, "###0.00")

Dim Tax As MyChannel.Tax = New MyChannel.Tax()

DR = Tax.GetTaxType

Me.DDLFreightTaxType.DataSource = DR

Me.DDLFreightTaxType.DataValueField = "taxtype"

Me.DDLFreightTaxType.DataTextField = "taxcode"

Me.DDLFreightTaxType.DataBind()

DR.Close()

Me.DDLFreightTaxType.SelectedIndex =
GetDDLItemIndexByValue(Me.DDLFreightTaxType,
CStr(MyOrderDetails.FreightTaxType))

Me.lblExTaxTotal.Text = Format(MyOrderDetails.OrderTotalExTax, "###0.00")

Me.lblTotal.Text = Format(MyOrderDetails.OrderTotal, "###0.00")

Me.lblTax.Text = Format(MyOrderDetails.OrderTotal -
MyOrderDetails.OrderTotalExTax, "###0.00")

Me.dgOrderDetails.DataSource = MyOrderDetails.OrderItems.Tables(0)

Me.dgOrderDetails.DataBind()

End Sub
'MyOrderDetails class is populated as follows:

Public Function GetOrderDetails(ByVal orderID As Integer) As OrderDetails

' Create Instance of Connection and Command Object

Dim myConnection As New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))

Dim myCommand As New SqlDataAdapter("OrdersDetail", myConnection)

' Mark the Command as a SPROC

myCommand.SelectCommand.CommandType = CommandType.StoredProcedure

' Add Parameters to SPROC

Dim parameterOrderID As New SqlParameter("@OrderID", SqlDbType.Int, 4)

parameterOrderID.Value = orderID

parameterOrderID.Direction = ParameterDirection.InputOutput

myCommand.SelectCommand.Parameters.Add(parameterOr derID)

Dim parameterCustomerID As New SqlParameter("@CustomerID", SqlDbType.Int, 4)

parameterCustomerID.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterCu stomerID)

Dim parameterOrderDate As New SqlParameter("@OrderDate", SqlDbType.DateTime,
8)

parameterOrderDate.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterOr derDate)

Dim parameterShipDate As New SqlParameter("@ShipDate", SqlDbType.DateTime,
8)

parameterShipDate.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterSh ipDate)

Dim parameterOrderTotal As New SqlParameter("@OrderTotal", SqlDbType.Money,
8)

parameterOrderTotal.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterOr derTotal)

Dim parameterOrderTotalExTax As New SqlParameter("@OrderTotalExTax",
SqlDbType.Money, 8)

parameterOrderTotalExTax.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterOr derTotalExTax)

Dim parameterDelAddLine1 As New SqlParameter("@deladdline1",
SqlDbType.NVarChar, 50)

parameterDelAddLine1.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterDe lAddLine1)

Dim parameterDelAddLine2 As New SqlParameter("@deladdline2",
SqlDbType.NVarChar, 50)

parameterDelAddLine2.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterDe lAddLine2)

Dim parameterDelAddLine3 As New SqlParameter("@deladdline3",
SqlDbType.NVarChar, 50)

parameterDelAddLine3.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterDe lAddLine3)

Dim parameterDelAddLine4 As New SqlParameter("@deladdline4",
SqlDbType.NVarChar, 50)

parameterDelAddLine4.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterDe lAddLine4)

Dim parameterInvoiceNo As New SqlParameter("@invoiceno", SqlDbType.NVarChar,
8)

parameterInvoiceNo.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterIn voiceNo)

Dim parameterCustomerPO As New SqlParameter("@customerpo",
SqlDbType.NVarChar, 20)

parameterCustomerPO.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterCu stomerPO)

Dim parameterShipVia As New SqlParameter("@shipvia", SqlDbType.NVarChar, 20)

parameterShipVia.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterSh ipVia)

Dim parameterComment As New SqlParameter("@comment", SqlDbType.NVarChar,
255)

parameterComment.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterCo mment)

Dim parameterFreight As New SqlParameter("@freight", SqlDbType.Money, 8)

parameterFreight.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterFr eight)

Dim parameterTaxTypeOnFreight As New SqlParameter("@taxtypeonfreight",
SqlDbType.Int, 4)

parameterTaxTypeOnFreight.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterTa xTypeOnFreight)

Dim parameterOrderStatus As New SqlParameter("@Orderstatus", SqlDbType.Int,
4)

parameterOrderStatus.Direction = ParameterDirection.Output

myCommand.SelectCommand.Parameters.Add(parameterOr derStatus)

' Create and Fill the DataSet

Dim myDataSet As New DataSet()

myCommand.Fill(myDataSet, "OrderDetails")

' ship date is null if order doesn't exist, or belongs to a different user

If Not parameterShipDate.Value Is DBNull.Value Then

' Create and Populate OrderDetails Struct using

' Output Params from the SPROC, as well as the

' populated dataset from the SqlDataAdapter

Dim myOrderDetails As New OrderDetails()

myOrderDetails.OrderDate = CType(parameterOrderDate.Value, DateTime)

myOrderDetails.ShipDate = CType(parameterShipDate.Value, DateTime)

myOrderDetails.OrderTotal = CDec(parameterOrderTotal.Value)

myOrderDetails.OrderItems = myDataSet

myOrderDetails.OrderID = orderID

myOrderDetails.CustomerID = CInt(parameterCustomerID.Value)

myOrderDetails.DelAddLine1 = CStr(parameterDelAddLine1.Value)

myOrderDetails.DelAddLine2 = CStr(parameterDelAddLine2.Value)

myOrderDetails.DelAddLine3 = CStr(parameterDelAddLine3.Value)

myOrderDetails.DelAddLine4 = CStr(parameterDelAddLine4.Value)

myOrderDetails.InvoiceNo = CStr(parameterInvoiceNo.Value)

myOrderDetails.CustomerPO = CStr(parameterCustomerPO.Value)

myOrderDetails.ShipVia = CStr(parameterShipVia.Value)

myOrderDetails.Comment = CStr(parameterComment.Value)

myOrderDetails.Freight = CDec(parameterFreight.Value)

myOrderDetails.FreightTaxType = CInt(parameterTaxTypeOnFreight.Value)

myOrderDetails.OrderStatus = CInt(parameterOrderStatus.Value)

myOrderDetails.OrderTotalExTax = CDec(parameterOrderTotalExTax.Value)

' Return the DataSet

Return myOrderDetails

Else

Return Nothing

End If

End Function

'The OrdersDetails class is defined as follows:

Public Class OrderDetails

Public OrderDate As DateTime

Public ShipDate As DateTime

Public OrderTotal As Decimal

Public OrderItems As DataSet

Public OrderID As Integer

Public CustomerID As Integer

Public DelAddLine1 As String

Public DelAddLine2 As String

Public DelAddLine3 As String

Public DelAddLine4 As String

Public InvoiceNo As String

Public CustomerPO As String

Public ShipVia As String

Public Comment As String

Public Freight As Decimal

Public FreightTaxType As Integer

Public OrderStatus As Integer

Public OrderTotalExTax As Decimal

End Class

'the stored proc OrdersDetails is shown below for the sake completeness

CREATE Procedure OrdersDetail
(
@OrderID int output,
@CustomerID int output,
@OrderDate datetime OUTPUT,
@ShipDate datetime OUTPUT,
@OrderTotal money OUTPUT,
@OrderTotalExTax money output,
@deladdline1 nvarchar(50) output,
@deladdline2 nvarchar(50) output,
@DelAddLine3 nvarchar(50) output,
@DelAddLine4 nvarchar(50) output,
@invoiceno nvarchar(8) output,
@customerpo nvarchar(20) output,
@shipvia nvarchar(20) output,
@comment nvarchar(255) output,
@freight money output,
@taxtypeonfreight int output,
@orderstatus int output
)
AS

/* Return the order dates from the Orders
Also verifies the order exists for this customer. */
SELECT
@CustomerID = CustomerID,
@OrderDate = OrderDate,
@ShipDate = ShipDate,
@deladdline1=deladdline1,
@deladdline2=deladdline2,
@deladdline3=deladdline3,
@deladdline4=deladdline4,
@invoiceno=invoiceno,
@customerpo=customerpo,
@shipvia=shipvia,
@comment=comment,
@freight=freight,
@taxtypeonfreight=taxtypeonfreight,
@orderstatus=orderstatus
FROM
Orders

WHERE
OrderID = @OrderID
IF @@Rowcount = 1
BEGIN

/* First, return the OrderTotal out param */
select
@ordertotal=convert(money,isnull((
select
sum(b.quantity*b.unitcost*(1+c.taxrate))
from
orderdetails b inner join tax c on
b.linetaxtype=c.taxtype
where
b.orderid=a.orderid
),0)+a.freight*(1+d.taxrate)),
@ordertotalextax=convert(money,isnull((
select
sum(b.quantity*b.unitcost)
from
orderdetails b inner join tax c on
b.linetaxtype=c.taxtype
where
b.orderid=a.orderid
),0)+a.freight)
from
orders a inner join tax d on
a.taxtypeonfreight=d.taxtype
where
a.orderid=@orderid and
a.customerid=@customerid

/* Then, return the recordset of info */
SELECT
OrderDetails.OrderLineID,
Products.ProductID,
Products.ModelNumber,
OrderDetails.LineDescription,
OrderDetails.UnitCost,
OrderDetails.Quantity,
(OrderDetails.Quantity * OrderDetails.UnitCost) as ExtendedAmount,
LineTaxType
FROM
OrderDetails
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID

WHERE
OrderID = @OrderID

END
else
-- if @orderid doesn't exist then set @orderid to 0 to signal it
set @orderid=0

GO
As the code above shows, there is no reference to a datareader object at
all. Any idea what is going on?


Nov 18 '05 #1
0 1562

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brent Burkart | last post by:
Below is the error I am receiving. I have checked SQL Profiler and it is receiving the correct query which runs fine in Query Analyzer. Any ideas? Server Error in '/lockinsheet' Application....
0
by: CW | last post by:
I get this FieldCount error when I attempt to bind a datagrid with a dataset, not a datareader object. The code snippet is as belows: 'PopulateForm called in Page Load event Private Sub...
2
by: Patrick Olurotimi Ige | last post by:
Why do i get "Invalid attempt to FieldCount when reader is closed" Is the problem the way the datareader reads data as opposed to a dataset? When trying to compile this code:- Dim reader As...
3
by: Patrick Olurotimi Ige | last post by:
With the code below i get error:- Invalid attempt to read data when reader is closed. //Get a datareader SqlDataReader objDataReader; objDataReader =...
4
by: Dave | last post by:
I'm using a datareader to get data from an sql table. The line that gives the error is as follow, dtrReceivers.ToString() which gives the error, Invalid attempt to read when no data is...
1
by: sivam.solai | last post by:
Public Function GetTopLevelThreadName(ByVal ThreadID As Integer) As SqlDataReader Sp_Datasql = "WS_Get_TopLevelThreadName" SqlParam = New SqlParameter(0) {} Try SqlParam(0) = New...
4
by: MarkusR | last post by:
If I run the stored proc in the Query Analyzer this works and I get the expected result set back. However when I run it in my application I get a results set of one row but when I try to access the...
2
by: Chris | last post by:
Hi, i wrote this code for fetching data from a table using a stored procedure. The connection definition and opening are put in a class. The actual read of data occurs in code-behind. My...
1
by: mark.norgate | last post by:
Hi I have a problem with a repeater and a data connection. In my Page_Load, I have the following code: Dim connectionString As String = WebConfig.ConnectionString Dim connection As...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.