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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:54:56+00:00 2026-05-29T23:54:56+00:00

One of the guys working on a project with me keeps getting an error

  • 0

One of the guys working on a project with me keeps getting an error when the code trys to validate if SqlDataReader returns a null value in a column. He runs this code:

if (!DB_Conn.isConnected()) DB_Conn.Connect();
using (SqlDataReader dr = DB_Conn.QueryDB(query))
{
   if (dr.HasRows && !dr.IsDBNull(0))
   {
       maxID = dr.GetInt32(0);
   }
}

But gets an error that Its an invalid attemtp to read when no data is present at the !dr.IsDBNull(0) command.

If i run this same code but i query a different table, it works.

Also, i run both queries and they return the expected null value. The querys are:

SELECT MAX(ID) FROM Loan;
SELECT MAX(ID) FROM InternationalSwap;

I dont think the querys have any affect on the reason why we are getting this error at one machine and not the other.

  • 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-05-29T23:54:57+00:00Added an answer on May 29, 2026 at 11:54 pm

    You need to call the Read method before trying to access any columns:

    while (dr.Read())
    {
        if (!dr.IsDBNull(0))
            maxID = dr.GetInt32(0);
    }
    

    But… if you only need a single value then you should probably use something like ExecuteScalar rather than a datareader.

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

Sidebar

Related Questions

Okay guys, I am working hashmap, using one of my classes as Key. My
Hey guys, I'm working on a status-Updater. It works, there is just one problem,
I'm working on a project where one of the co-developers (and previous developers) use
Hi guys we have following structure in one of our project's pages: <asp:UpdatePanel ID=up1
I am working on bar code reader in one of the application. I am
I've searched and searched and I'm coming up empty on this one guys. Help!
Guys in one of excersises (ch.5,e.8) from TC++PL Bjarne asks to do following: 'Run
Hey guys I have one more question lol. I am using a script that
Hi guys i am trying to create one simple make file and execute in
right guys im having trouble with one. what i want to do is have

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.