472,782 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 3673
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.