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 8035735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:23:38+00:00 2026-06-05T02:23:38+00:00

My code: string sqlQuery1 = select * from employees where depid=6; SqlDataReader Dr1 =

  • 0

My code:

string sqlQuery1 = "select * from employees where depid=6";
SqlDataReader Dr1 = dbconn.RunQueryReturnDataReader(sqlQuery1);

if(Dr1.read()) {    //or if (Dr1["empname"] != DBNull.Value)

            while (Dr1.Read())
            {        
                Label1.Text = Dr1["empname"].ToString();  
                Label2.Text = Dr1["empdes"].ToString(); 
                ...

            }
            Dr1.Close();
 }
 else {
            Label1.text = "defaultValue";
            Label2.text = "defaultValue";
            ...
 }

I just want to check SqlDataReader has no records to display. If no records ,the labels should showdefault values preassigned by me or If has record display datareader value on label. (Assume either SqlDataReader has 1 recode or has norecord only)

How can I check first datareader hasRow or not?

I have tried two ways as above code.

  1. if(Dr1.read()) – this way working fine. But after execute If statement ,it has no value to display on labels , since read() will increase the pointer. As a result label1 ,Label2.. Nothing display.

  2. if (Dr1["empname"] != DBNull.Value)

    This way generate an exception when Sqldatareader has norows.

error: System.InvalidOperationException: Invalid attempt to read when no data is present

Please help me. tx

  • 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-05T02:23:40+00:00Added an answer on June 5, 2026 at 2:23 am

    try…

    if(Dr1.HasRows)
    {
       //....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my code: string strSQL = SELECT * from tMedia where SKU = '
In this code: string sql = SELECT * FROM rd_main WHERE manifest!=\\ AND manifest
I am using a SQLDataReader to read a limited number of rows from a
I'm currently using EF Code-First, and I'd like to SELECT data from many tables
The code: String s = \d; raises an compiler error, illegal escape character. Ok
I've got this code: string test(żaba); cout << Word: << test << endl; cout
I have the following code: string input = ç; string normalized = input.Normalize(NormalizationForm.FormD); char[]
I have some code: string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string pathDownload = Path.Combine(pathUser, @documents\iracing\setups\); DirectoryInfo
Here's the code: string name = myName; int id = (int)_myDB.ThingTable.Where(thing => thing.ThingName ==
I have the following code string three() { return three; } void mutate(string& ref)

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.