473,748 Members | 6,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(By Val MyOrderDetails As MyChannel.Order Details)

Me.lblOrderID.T ext = CStr(MyOrderDet ails.OrderID)

Dim customer As MyChannel.Custo mersDB = New MyChannel.Custo mersDB()

Dim DR As SqlClient.SqlDa taReader =
customer.GetCus tomerList(CInt( Me.Page.User.Id entity.Name))

Me.DDLCustomer. DataSource = DR

Me.DDLCustomer. DataValueField = "CustomerID "

Me.DDLCustomer. DataTextField = "FullName"

Me.DDLCustomer. DataBind()

DR.Close()

Me.DDLCustomer. SelectedIndex = GetDDLItemIndex ByValue(Me.DDLC ustomer,
CStr(MyOrderDet ails.CustomerID ))

Me.lblOrderDate .Text = Format(MyOrderD etails.OrderDat e, "d")

Me.tbCustomerPO .Text = MyOrderDetails. CustomerPO

Me.tbDelAddLine 1.Text = MyOrderDetails. DelAddLine1

Me.tbDelAddLine 2.Text = MyOrderDetails. DelAddLine2

Me.tbDelAddLine 3.Text = MyOrderDetails. DelAddLine3

Me.tbDelAddLine 4.Text = MyOrderDetails. DelAddLine4

Me.tbShipDate.T ext = Format(MyOrderD etails.ShipDate , "d")

Me.tbShipVia.Te xt = MyOrderDetails. ShipVia

Me.tbComments.T ext = MyOrderDetails. Comment

Me.tbFreight.Te xt = Format(MyOrderD etails.Freight, "###0.00")

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

DR = Tax.GetTaxType

Me.DDLFreightTa xType.DataSourc e = DR

Me.DDLFreightTa xType.DataValue Field = "taxtype"

Me.DDLFreightTa xType.DataTextF ield = "taxcode"

Me.DDLFreightTa xType.DataBind( )

DR.Close()

Me.DDLFreightTa xType.SelectedI ndex =
GetDDLItemIndex ByValue(Me.DDLF reightTaxType,
CStr(MyOrderDet ails.FreightTax Type))

Me.lblExTaxTota l.Text = Format(MyOrderD etails.OrderTot alExTax, "###0.00")

Me.lblTotal.Tex t = Format(MyOrderD etails.OrderTot al, "###0.00")

Me.lblTax.Text = Format(MyOrderD etails.OrderTot al -
MyOrderDetails. OrderTotalExTax , "###0.00")

Me.dgOrderDetai ls.DataSource = MyOrderDetails. OrderItems.Tabl es(0)

Me.dgOrderDetai ls.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(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))

Dim myCommand As New SqlDataAdapter( "OrdersDeta il", myConnection)

' Mark the Command as a SPROC

myCommand.Selec tCommand.Comman dType = CommandType.Sto redProcedure

' Add Parameters to SPROC

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

parameterOrderI D.Value = orderID

parameterOrderI D.Direction = ParameterDirect ion.InputOutput

myCommand.Selec tCommand.Parame ters.Add(parame terOrderID)

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

parameterCustom erID.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terCustomerID)

Dim parameterOrderD ate As New SqlParameter("@ OrderDate", SqlDbType.DateT ime,
8)

parameterOrderD ate.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terOrderDate)

Dim parameterShipDa te As New SqlParameter("@ ShipDate", SqlDbType.DateT ime,
8)

parameterShipDa te.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terShipDate)

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

parameterOrderT otal.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terOrderTotal)

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

parameterOrderT otalExTax.Direc tion = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terOrderTotalEx Tax)

Dim parameterDelAdd Line1 As New SqlParameter("@ deladdline1",
SqlDbType.NVarC har, 50)

parameterDelAdd Line1.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terDelAddLine1)

Dim parameterDelAdd Line2 As New SqlParameter("@ deladdline2",
SqlDbType.NVarC har, 50)

parameterDelAdd Line2.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terDelAddLine2)

Dim parameterDelAdd Line3 As New SqlParameter("@ deladdline3",
SqlDbType.NVarC har, 50)

parameterDelAdd Line3.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terDelAddLine3)

Dim parameterDelAdd Line4 As New SqlParameter("@ deladdline4",
SqlDbType.NVarC har, 50)

parameterDelAdd Line4.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terDelAddLine4)

Dim parameterInvoic eNo As New SqlParameter("@ invoiceno", SqlDbType.NVarC har,
8)

parameterInvoic eNo.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terInvoiceNo)

Dim parameterCustom erPO As New SqlParameter("@ customerpo",
SqlDbType.NVarC har, 20)

parameterCustom erPO.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terCustomerPO)

Dim parameterShipVi a As New SqlParameter("@ shipvia", SqlDbType.NVarC har, 20)

parameterShipVi a.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terShipVia)

Dim parameterCommen t As New SqlParameter("@ comment", SqlDbType.NVarC har,
255)

parameterCommen t.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terComment)

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

parameterFreigh t.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terFreight)

Dim parameterTaxTyp eOnFreight As New SqlParameter("@ taxtypeonfreigh t",
SqlDbType.Int, 4)

parameterTaxTyp eOnFreight.Dire ction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terTaxTypeOnFre ight)

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

parameterOrderS tatus.Direction = ParameterDirect ion.Output

myCommand.Selec tCommand.Parame ters.Add(parame terOrderStatus)

' Create and Fill the DataSet

Dim myDataSet As New DataSet()

myCommand.Fill( myDataSet, "OrderDetai ls")

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

If Not parameterShipDa te.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(parameter OrderDate.Value , DateTime)

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

myOrderDetails. OrderTotal = CDec(parameterO rderTotal.Value )

myOrderDetails. OrderItems = myDataSet

myOrderDetails. OrderID = orderID

myOrderDetails. CustomerID = CInt(parameterC ustomerID.Value )

myOrderDetails. DelAddLine1 = CStr(parameterD elAddLine1.Valu e)

myOrderDetails. DelAddLine2 = CStr(parameterD elAddLine2.Valu e)

myOrderDetails. DelAddLine3 = CStr(parameterD elAddLine3.Valu e)

myOrderDetails. DelAddLine4 = CStr(parameterD elAddLine4.Valu e)

myOrderDetails. InvoiceNo = CStr(parameterI nvoiceNo.Value)

myOrderDetails. CustomerPO = CStr(parameterC ustomerPO.Value )

myOrderDetails. ShipVia = CStr(parameterS hipVia.Value)

myOrderDetails. Comment = CStr(parameterC omment.Value)

myOrderDetails. Freight = CDec(parameterF reight.Value)

myOrderDetails. FreightTaxType = CInt(parameterT axTypeOnFreight .Value)

myOrderDetails. OrderStatus = CInt(parameterO rderStatus.Valu e)

myOrderDetails. OrderTotalExTax = CDec(parameterO rderTotalExTax. 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,
@OrderTotalExTa x 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,
@taxtypeonfreig ht 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=de laddline1,
@deladdline2=de laddline2,
@deladdline3=de laddline3,
@deladdline4=de laddline4,
@invoiceno=invo iceno,
@customerpo=cus tomerpo,
@shipvia=shipvi a,
@comment=commen t,
@freight=freigh t,
@taxtypeonfreig ht=taxtypeonfre ight,
@orderstatus=or derstatus
FROM
Orders

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

/* First, return the OrderTotal out param */
select
@ordertotal=con vert(money,isnu ll((
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.ord erid
),0)+a.freight* (1+d.taxrate)),
@ordertotalexta x=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.ord erid
),0)+a.freight)
from
orders a inner join tax d on
a.taxtypeonfrei ght=d.taxtype
where
a.orderid=@orde rid and
a.customerid=@c ustomerid

/* Then, return the recordset of info */
SELECT
OrderDetails.Or derLineID,
Products.Produc tID,
Products.ModelN umber,
OrderDetails.Li neDescription,
OrderDetails.Un itCost,
OrderDetails.Qu antity,
(OrderDetails.Q uantity * OrderDetails.Un itCost) as ExtendedAmount,
LineTaxType
FROM
OrderDetails
INNER JOIN Products ON OrderDetails.Pr oductID = Products.Produc tID

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 1581

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

Similar topics

2
5211
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. ---------------------------------------------------------------------------- ---- Invalid attempt to read when no data is present. Description: An unhandled exception occurred during the execution of the current web request. Please review the...
0
1725
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 PopulateForm(ByVal MyOrderDetails As MyChannel.OrderDetails) Me.lblOrderID.Text = CStr(MyOrderDetails.OrderID)
2
5063
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 IDataReader = GetReader() Dim chart As New LineChart()
3
3034
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 = objCommand.ExecuteReader(CommandBehavior.CloseConnection); datalistoutput.DataSource = objDataReader;
4
12370
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 present which is correct. The line works when data is in the row. But shouldn't the
1
19771
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 SqlParameter("@ThreadID", SqlDbType.Int) SqlParam(0).Value = ThreadID
4
3475
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 values I get "Invalid attempt to read when no data is present". private void GetLotIDPriorityFromLot(string aLotDesc, out int aLotID, out DateTime aPriorityDate) { SqlConnection Conn; SqlDataReader reader = null;
2
6941
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 problem with this code: Invalid attempt to Read when reader is closed at line: While dtreader.Read() Thanks for help
1
5971
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 SqlConnection = New SqlConnection(connectionString)
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.