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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:03:22+00:00 2026-06-14T09:03:22+00:00

I have code similar to the following: SqlDataReader sdr = null; . . sdr

  • 0

I have code similar to the following:

SqlDataReader sdr = null;
.
.
sdr = cmd.ExecuteReader();

while(sdr.Read()){
    string x = sdr["x"].ToString();
    Console.WriteLine(x);
    long? y = Int64.Parse(sdr["y"].ToString());
}

and I am currently debugging a problem when parsing the value of y. In the database, for the value written to the console, x, it says y is a numeric value (and if it wasn’t- it would be null).

When the exception is thrown I hover over sdr and navigate to the actual data being stored. The values in element [0] do the correspond to the value x which has just been written to screen. In other words, x is definitely accessed, because I can see the correct value for x being displayed. However, in Visual Studio 2008, when debugging, looking at the object’s memory the first element does not have an x-column value the same as what was just displayed on console!

-Why can I not see the element which has been accessed?
-Is this problem related to the fact my numeric value isn’t getting parsed correctly?

I did notice another strange thing: The first time I look at the sdr object in Visual Studio I can see the elements. If i move the mouse to the other side of the screen and then hover back over the object, try to iterate through the elements of the sdr reader object, there are no elements- as if it is empty?!?!

  • 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-14T09:03:23+00:00Added an answer on June 14, 2026 at 9:03 am

    Really that’s not the way to work with SqlDataReader…

    Putting apart the compilation problem stated by @TimSchmelter you should write code that checks for DBNull.Value before trying to access and convert the values of a SqlDataReader

    string x = string.Empty;
    int index = sdr.GetOrdinal("x");
    if (!sdr.IsDBNull(index))
         x = sdr.GetString(index);
    
    long? y;
    index = sdr.GetOrdinal("y");
    if (!sdr.IsDBNull(index))
        y = sdr.GetInt64(index);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have code similar to the following: while(myDataReader.Read()) { myObject.intVal = Convert.ToInt32(myDataReader[mycolumn] ??
I have seen sample code similar to the following: std::string s = Hello World!;
I have a line of code similar to the following: final String food =
I have code similar to the following: <div class=container> <div class=overlay> // hover content
I have code similar to the following with a URL like this... If I
In a CUDA kernel, I have code similar to the following. I am trying
I have code, similar to the following, that I would like to modify: Sub
I have a couple of libraries that use code similar to the following one.
I wrote the following loop since I have to repeat the same/similar code 25
I have an ASP.NET web site with something similar to the following code: .aspx

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.