Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8270405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:33:35+00:00 2026-06-08T06:33:35+00:00

How would you write the code to read the data from a query and

  • 0

How would you write the code to read the data from a query and display each row in a separate label if you don’t know how many rows the query result may contain? If you try to read one too many rows with the DataReader, it throws an excetion when you try to read the column data on the row that doesn’t exist. I’m not sure how to code this.

If dr.HasRows Then
    dr.Read()
    LN2.Text = dr.Item("linenum").ToString
Else
    LN2.visible  = False         
End If

This example shows how I am loading the second row with the DataReader. This works if there are two rows of data, but if there is only one row of data, it throws an exception. I have a maximum of 12 rows of data but, but my actual query results may contain anywhere between 1 and 12 rows of data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:33:36+00:00Added an answer on June 8, 2026 at 6:33 am

    The Read method returns a boolean indicating whether or not the read of the next row was successful. Therefore, you could change your code to something like this:

    If dr.Read() Then
        LN2.Text = dr.Item("linenum").ToString
    Else
        LN2.visible  = False         
    End If
    

    However, usually, in such cases, displaying varying numbers of rows of data in a fixed number of labels isn’t the best approach. For instance, if you used a single ListBox control instead, all your loading code could be simplified to the following:

    While dr.Read()
        ListBox1.Items.Add(dr.Item("linenum").ToString())
    End While
    

    If you need to show multiple columns of data for each row, I would recommend using either a ListView control (with the View property set to Details) or a DataGridView control.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to read/write data from my Oracle DB into my iPhone code.
Can .NET (managed code) read and write to CouchDB? I would like to build
would someone please write this code: this.Loaded += (s, e) => this.loaded = true;
How would I write the following C# code in F#? namespace Shared { public
I need to write some code that would loop though all rows of a
This is as far as I've gotten. What code would I write to save
I would like to write some Objective-C code for distribution. What is the best
I'd like to write a function that would have some optional code to be
I am write a small python script to gather some data from a database,
In my code, I have a situation where I need to copy data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.