LINQ -Tried to get a count to see if query returned anything

The query results cannot be enumerated more than once
and the routine died on line
73:         Next

what I am trying to do is see if I've returned a recordset and, if not, instead of showing results, show a message to the effect that no results were returned.

I am VERY new to LINQ, watched videos on it, can't find the syntax to bind to a list (I saw the video, cannot FIND it again)... not even sure if I should try to bind to a list.

thanks for any help
Lynda

Dim rs = From Product In dConn.StProc_GetDetail(Request.QueryString("pn"), Request.QueryString("cID"))
        Dim iRowCount As Integer = rs.Count
        Response.Write(iRowCount.ToString)
        If rs.Count > 0 Then
            
        End If
        
        For Each Product In rs
            lblDesc1.Text = Left(Product.Desc1, InStr(Product.Desc1, ":") - 1)
            imgPartDetail.ImageUrl = Product.sImageLocation
            imgPartDetail.ImageAlign = ImageAlign.Left
            lblLongDescription.Text = Product.LongDescription
            lblAmount.Text = Product.dListPrice
            lblPartNo.Text = Product.sPartNo
            lblMainTitle.Text = Product.sMainTitle
            lblQtyAtDealer.Text = Product.IQtyAtDealer
            lblBike.Text = Product.sBike
            lblBikeRecNo.Text = Product.iBikeRecNo
            lbldlrEa.Text = Product.dlrEa
            lblListPrice.Text = Product.dListPrice
            lblRetail.Text = Product.Retail
            lblSubCategoryNo.Text = Product.CategoryID
            lblCost.Text = Product.dCost
            lblCategoryNo.Text = Product.CategoryID
            
        Next '<-- dies here

Open in new window

LyndaPostalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Göran AnderssonCommented:
The count method enumerates the result to count how many there are.

If you don't have to know the number of items before looping, you can count them while looping:

  Dim rowCount as Integer = 0
  For Each Product in rs
    ...
    rowCount += 1
  Next
  If rowCount = 0 Then
    ' no items
  End If

If you want to use the result more than once, you can read it into a list, and then use the list. As the list has a Count property it's more efficient to use that than using the Count() method to iterate the collection and count the items:

  var items = rs.ToList()
  If items.Count = 0 Then
    ' no items
  End If
  For Each Product in items
    ...
  Next
bhmahlerCommented:
OR you can just put a boolean flag in you for each loop to see if there are any records. Something like this.

Dim rs = From Product In dConn.StProc_GetDetail(Request.QueryString("pn"), Request.QueryString("cID"))
        
Dim hasRecords As Boolean = False
        
For Each Product In rs
    lblDesc1.Text = Left(Product.Desc1, InStr(Product.Desc1, ":") - 1)
    imgPartDetail.ImageUrl = Product.sImageLocation
    imgPartDetail.ImageAlign = ImageAlign.Left
    lblLongDescription.Text = Product.LongDescription
    lblAmount.Text = Product.dListPrice
    lblPartNo.Text = Product.sPartNo
    lblMainTitle.Text = Product.sMainTitle
    lblQtyAtDealer.Text = Product.IQtyAtDealer
    lblBike.Text = Product.sBike
    lblBikeRecNo.Text = Product.iBikeRecNo
    lbldlrEa.Text = Product.dlrEa
    lblListPrice.Text = Product.dListPrice
    lblRetail.Text = Product.Retail
    lblSubCategoryNo.Text = Product.CategoryID
    lblCost.Text = Product.dCost
    lblCategoryNo.Text = Product.CategoryID
    hasRecords = True
Next

If Not hasRecords Then
    'Show your no records message here
End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LyndaPostalAuthor Commented:
I like the idea of the Boolean. also the first solution was 'mixed languages.; c# AND vb, sort of. As I read it, 'var' something resolves to 'dim' something. I can read (and translate) c#, but I don't write in it. Between VB and SQL, the occasional PHP and javascript, I'm having just about as much fun as I can stand. Both good solutions, the second being more of what I was looking for. Thank you so much for your time and effort. :) Lynda
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.