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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:58:48+00:00 2026-06-07T23:58:48+00:00

I have faced some interesting situation. I have the following code to populate value

  • 0

I have faced some interesting situation. I have the following code to populate value of field from DB (ASP.net):

SqlConnection connect =
            new SqlConnection(
                @"conn-string"); 
SqlCommand toDo = new SqlCommand(InfoQuery, connect);
        toDo.CommandTimeout = 6000;
        connect.Open();
        using (SqlDataReader reader = toDo.ExecuteReader())
        {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    ///......retrieving some fields in the same way as below
                    foo = reader.SafeGetString(7);
                    int temp = reader.SafeGetInt32(8);
                    ///......retrieving again
                }
        }
        connect.close()

The connection is established, all params are correct. In SQL Server Management Studio the query associated with toDo command works just perfect. In program when running every field to the temp value (not including temp) is retrieved and set. But when reading temp value, i get the following exception:

Invalid attempt to read when no data is present.

And here are my extension methods:

public static class ExtentionMethods
{
    public static string SafeGetString(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetString(colIndex);
        return "NULL VALUE";
    }

    public static int SafeGetInt32(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetInt32(colIndex);
        return -1;
    }

    public static DateTime SafeGetDateTime(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
        {
            try
            {
            }
            catch
            {
                return new DateTime(1800, 1, 1);
            }
        }
        return new DateTime(1800, 1, 1);
    }
}

Query:

SELECT TOP 1000 [ID0]
        ,[Id1]
        ,[Id2]
        ,Id1+Id2+'0' AS BC
        ,[Id3]
        ,[Id4]
        ,[Id5]
        ,CAST([Date] AS nvarchar(max))
        ,[int]
        ,[Id7]
        ,[Id8]
        ,IsNull(foo,'dsf')+' '+IsNull(bar,'dsf')+', '
        +IsNull(fgm,'fggf')+', fgfggf '+IsNull(gfgf,'gfgf')+
        ','+dfsdfsdsf+', '+dsffddf AS dsadsa
        ,[fg]
        ,[fds]
        FROM tbl
        inner join tbl1 on tbl1.ID=ID1
        inner join tbl2 on tbl2.ID=ID2
        WHERE Id4=12

What the problem could be?

  • 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-07T23:58:50+00:00Added an answer on June 7, 2026 at 11:58 pm

    I don’t think you need all this extensions methods, and you should access columns by name :

    while (reader.Read())
    {
        int intCol = reader["MyIntColumn"] as int? ?? -1;
        string stringCol = reader["MyStringColumn"] as string ?? "NULL VALUE";
        DateTime dateCol = reader["MyDateColumn"] as DateTime? ?? new DateTime(1800, 1, 1);
    }
    

    int? or DateTime? will allow nulls and ?? will affect a default value if the column is null.

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

Sidebar

Related Questions

Today I faced a strange problem in C#. I have an ASP.NET page where
i have faced some problem while navigating from one class to another with the
I am slowly moving into Silverlight from asp.net and have a question about how
I have faced a problem in Qt with error: cannot convert parameter 1 from
I faced an interesting problem today. I have 4 strings which I need to
I'm solving simple math task, and I have faced with some problems. I have
i have faced some problem , when i want to display some static Title
I have been making a project on Android 4.0 and I have faced some
I´m using ASP.NET 4.0 and testing the ASP.NET XHTML compatibility with some components and
Hope some of you might have faced this requirement:- I have desktop based product,

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.