473,406 Members | 2,849 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,406 software developers and data experts.

Retrieve Record Count

A SQL Server 2005 stored procedure expects a parameter 'UserID'
depending upon which it retrieves the no. of records & OrderIDs
corresponding to the 'UserID' from a DB table (note that OrderID &
UserID are two of the columns in the DB table). So for e.g. consider a
user whose UserID=6 & the DB table has 3 records whose UserID=6. In
other words, there are 3 OrderID records of the user whose UserID=6,
say, OrderID=8, OrderID=17 & OrderID=29. The stored procedure will
finally return 2 columns - the OrderCount (which is 3 for UserID=6) &
the OrderID (which will be 8, 17 & 29 for UserID=6). This is the stored
procedure:

ALTER PROCEDURE dbo.NETAllOrders
@UserID int
AS
DECLARE
@OrderCount int

SET @OrderCount = (SELECT COUNT(OrderID) FROM NETOrders WHERE UserID =
@UserID)

SELECT @OrderCount AS OrderCount, OrderID
FROM
NETOrders
WHERE
UserID = @UserID

In a VB class file, I am invoking the stored procedure in a function
named 'GetOrderCount' which returns a SqlDataReader back to the calling
ASPX page. I want the ASPX page to display the 3 OrderIDs of UserID=6
in a Label control. Unlike the DataBinding controls like DataList,
DataGrid, Repeater controls, the Label control will not automatically
loop through the resultset.

So I tried to accomplish this using a For...Next loop

Dim i As Integer
Dim sqlReader As SqlDataReader

iUserID = Request.Cookies("UserID").Value
'ShopCart is the name of the class in the VB class file
boShopCart = New ShopCart
sqlReader = boShopCart.GetOrderCount(iUserID)

While (sqlReader.Read)
If (sqlReader.GetValue(0) 1) Then
pnlLinks.Visible = True
For i = 1 To sqlReader.GetValue(0)
lblLinks.Text = sqlReader.GetValue(1)(i)
Next i
Else
pnlLinks.Visible = False
End If
End While

But this generates the following error:

No default member found for type 'Integer'.

pointing to the line

lblLinks.Text = sqlReader.GetValue(1)(i)

Can someone please correct me & suggest how do I loop through the
resultset so that I can display the records in a Label control?

Oct 17 '06 #1
2 3700
You don't need to do it in the fashion you are. For one thing, when
assigning a variable to be a count, just use the select.

SELECT @OrderCount = COUNT(OrderID) FROM NETOrders WHERE UserID = @UserID

SELECT @OrderCount as OrderCount, OrderId FROM NETOrders WHERE UserID =
@UserID

The reason you're getting the error comes from the fact that @OrderCount
wasn't initialised to any sort of value. Then the set statement didn't
actually assign it a value since it needed to be done as part of a select,
so the integer still remained uninitialized.

If you're going to loop through records to add the values to a label
control, you can't assign them the way you're doing it. Label.Text =
somevalue will just keep setting the label's text to a new value each time.
You need to set the label's text property to be equal to the current label's
text property, plus the new value.

For example:

lblLinks.Text = (lblLinks.Text & " " & sqlReader.GetValue(1)(i))

That's roughly it, though I could be off as I'm used to C# syntax. I added a
space in there in the concatenation to make it a little easier to read.
--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

<rn**@rediffmail.comwrote in message
news:11*********************@f16g2000cwb.googlegro ups.com...
>A SQL Server 2005 stored procedure expects a parameter 'UserID'
depending upon which it retrieves the no. of records & OrderIDs
corresponding to the 'UserID' from a DB table (note that OrderID &
UserID are two of the columns in the DB table). So for e.g. consider a
user whose UserID=6 & the DB table has 3 records whose UserID=6. In
other words, there are 3 OrderID records of the user whose UserID=6,
say, OrderID=8, OrderID=17 & OrderID=29. The stored procedure will
finally return 2 columns - the OrderCount (which is 3 for UserID=6) &
the OrderID (which will be 8, 17 & 29 for UserID=6). This is the stored
procedure:

ALTER PROCEDURE dbo.NETAllOrders
@UserID int
AS
DECLARE
@OrderCount int

SET @OrderCount = (SELECT COUNT(OrderID) FROM NETOrders WHERE UserID =
@UserID)

SELECT @OrderCount AS OrderCount, OrderID
FROM
NETOrders
WHERE
UserID = @UserID

In a VB class file, I am invoking the stored procedure in a function
named 'GetOrderCount' which returns a SqlDataReader back to the calling
ASPX page. I want the ASPX page to display the 3 OrderIDs of UserID=6
in a Label control. Unlike the DataBinding controls like DataList,
DataGrid, Repeater controls, the Label control will not automatically
loop through the resultset.

So I tried to accomplish this using a For...Next loop

Dim i As Integer
Dim sqlReader As SqlDataReader

iUserID = Request.Cookies("UserID").Value
'ShopCart is the name of the class in the VB class file
boShopCart = New ShopCart
sqlReader = boShopCart.GetOrderCount(iUserID)

While (sqlReader.Read)
If (sqlReader.GetValue(0) 1) Then
pnlLinks.Visible = True
For i = 1 To sqlReader.GetValue(0)
lblLinks.Text = sqlReader.GetValue(1)(i)
Next i
Else
pnlLinks.Visible = False
End If
End While

But this generates the following error:

No default member found for type 'Integer'.

pointing to the line

lblLinks.Text = sqlReader.GetValue(1)(i)

Can someone please correct me & suggest how do I loop through the
resultset so that I can display the records in a Label control?

Oct 17 '06 #2
Well Mark.......I am sorry to say that I forgot to mention one very
important point in post #1. I want to display the 3 OrderIDs of
UserID=6 in the Label control but the Text of the Label shouldn't be
the OrderIDs themselves. In other words, the Label shouldn't display

8 17 29

which are the 3 OrderIDs of UserID=6. Rather the Text of the Label
control, which will reflect how many orders a user has placed, should
start from 1 & increment by 1 till the total number of orders a user
has placed. For e.g. since UserID=6 has 3 OrderIDs, it means that
UserID=6 has placed 3 orders - the OrderID of the first order being 8,
the OrderID of the second order being 17 & the OrderID of the third
order being 29. Hence the Label control should display

1 2 3

to UserID=6 since he has placed 3 orders. Similarly, if a user has
placed, say, 8 orders, the Label should display

1 2 3 4 5 6 7 8

& not the OrderIDs of the 8 orders. That's precisely the reason why I
used the For....Next loop in the code shown in post #1 but the problem
is since the For....Next loop has to be within a While loop to read the
SqlDataReader....something like this:

While (sqlReader.Read)
For i = 1 To sqlReader.GetValue(0)
lblLinks.Text += i & " "
Next
End While

the Label control displays the value of 'i' more than once since its
looping inside a loop. So for UserID=6, the Label control displays the
following Text:

1 2 3 1 2 3 1 2 3

Any idea how do I resolve this problem or any other suggestiona?

Thanks
Mark Fitzpatrick wrote:
You don't need to do it in the fashion you are. For one thing, when
assigning a variable to be a count, just use the select.

SELECT @OrderCount = COUNT(OrderID) FROM NETOrders WHERE UserID = @UserID

SELECT @OrderCount as OrderCount, OrderId FROM NETOrders WHERE UserID =
@UserID

The reason you're getting the error comes from the fact that @OrderCount
wasn't initialised to any sort of value. Then the set statement didn't
actually assign it a value since it needed to be done as part of a select,
so the integer still remained uninitialized.

If you're going to loop through records to add the values to a label
control, you can't assign them the way you're doing it. Label.Text =
somevalue will just keep setting the label's text to a new value each time.
You need to set the label's text property to be equal to the current label's
text property, plus the new value.

For example:

lblLinks.Text = (lblLinks.Text & " " & sqlReader.GetValue(1)(i))

That's roughly it, though I could be off as I'm used to C# syntax. I added a
space in there in the concatenation to make it a little easier to read.
--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

<rn**@rediffmail.comwrote in message
news:11*********************@f16g2000cwb.googlegro ups.com...
A SQL Server 2005 stored procedure expects a parameter 'UserID'
depending upon which it retrieves the no. of records & OrderIDs
corresponding to the 'UserID' from a DB table (note that OrderID &
UserID are two of the columns in the DB table). So for e.g. consider a
user whose UserID=6 & the DB table has 3 records whose UserID=6. In
other words, there are 3 OrderID records of the user whose UserID=6,
say, OrderID=8, OrderID=17 & OrderID=29. The stored procedure will
finally return 2 columns - the OrderCount (which is 3 for UserID=6) &
the OrderID (which will be 8, 17 & 29 for UserID=6). This is the stored
procedure:

ALTER PROCEDURE dbo.NETAllOrders
@UserID int
AS
DECLARE
@OrderCount int

SET @OrderCount = (SELECT COUNT(OrderID) FROM NETOrders WHERE UserID =
@UserID)

SELECT @OrderCount AS OrderCount, OrderID
FROM
NETOrders
WHERE
UserID = @UserID

In a VB class file, I am invoking the stored procedure in a function
named 'GetOrderCount' which returns a SqlDataReader back to the calling
ASPX page. I want the ASPX page to display the 3 OrderIDs of UserID=6
in a Label control. Unlike the DataBinding controls like DataList,
DataGrid, Repeater controls, the Label control will not automatically
loop through the resultset.

So I tried to accomplish this using a For...Next loop

Dim i As Integer
Dim sqlReader As SqlDataReader

iUserID = Request.Cookies("UserID").Value
'ShopCart is the name of the class in the VB class file
boShopCart = New ShopCart
sqlReader = boShopCart.GetOrderCount(iUserID)

While (sqlReader.Read)
If (sqlReader.GetValue(0) 1) Then
pnlLinks.Visible = True
For i = 1 To sqlReader.GetValue(0)
lblLinks.Text = sqlReader.GetValue(1)(i)
Next i
Else
pnlLinks.Visible = False
End If
End While

But this generates the following error:

No default member found for type 'Integer'.

pointing to the line

lblLinks.Text = sqlReader.GetValue(1)(i)

Can someone please correct me & suggest how do I loop through the
resultset so that I can display the records in a Label control?
Oct 17 '06 #3

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

Similar topics

6
by: 6thirty | last post by:
Hi, I've created a stocktaking database using Access XP. This is indexed by two fields - part number and shelf location. I am currently inputting all the data via a form. When I have entered a...
1
by: DaveF | last post by:
How can I retrieve display_names Dim custDS As DataSet = New DataSet Dim mySQL, mySQL2, mySQL3 As String
13
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end...
2
by: Loras | last post by:
Hello all, I'm a newbie in Access vba, so please help... I have some vba code to count records, but i also need retrieve value from query SELECT SUM (......). There will be only one record (Total...
4
by: Simon Gare | last post by:
Hi all, I am trying to retrieve a count of booking entries made 30 days ago, below is the end of the query I am having problems with. dbo.booking_form.TimeOfBooking = DATEADD(day, -30,...
14
by: David | last post by:
Hi, I have a form on which a user can select a checkbox against a record. Each checkbox carries the RecordID of a product. ---------------------------------------------------------------- I...
0
by: tigger | last post by:
Hi there, I'm having problems retrieving data from Access database using Visual basic 2005. My database is called Actual_Database and it has a few tables. One of them is called...
6
by: BSB | last post by:
Hi I use VB 6.o as my front end... execute a query in Access... by the following code sQueryName = "" adocmd.CommandText = sQueryName adocmd.CommandType = adCmdStoredProc ...
4
by: rakeshop86 | last post by:
I have a query like this... rs.Open "select * from Tab_OrderStat_samp where wrkSlab=" & CmbOrdDt & "", cn, adOpenStatic, adLockOptimistic but when a date is selected in the combo box which is there...
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.