Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. IDataReader and Null values

IDataReader and Null values

Scheduled Pinned Locked Moved C#
databasequestioncsharpasp-nethelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Terick
    wrote on last edited by
    #1

    Hi, I am using an IDataReader with SQL and ASP.Net. I keep getting an IndexOutOfRangeException because in one of the columns there is only a value for some of the rows (ex. if the size is over 0.5 there is a value, but if it is 0.5 or under that column is blank). How can I do this? So far I have:

    public static List GetAllNames(){
    SqlDatabase db = DatabaseFactory.CreateDatabase() as SqlDatabase;
    DbCommand cmd = db.GetStoredProcedureCommand("dbo.GetAllNames");

    List namelist= new List();

    using (IDataReader reader = db.ExecuteReader(cmd))
    {
    while(reader.Read())
    {
    namelist.Add(GetNameFromReader(reader));
    }
    }
    return namelist;
    }

    private static Name GetNameFromReader(IDataReader reader)
    {
    Name name_obj = new Name(
    (int)reader["ID"],
    ((reader)["size"] !=DBNull.Value)? (int)reader["size"] : (int)0));
    return name_obj;
    }

    Thanks so much for your help!

    P V 2 Replies Last reply
    0
    • T Terick

      Hi, I am using an IDataReader with SQL and ASP.Net. I keep getting an IndexOutOfRangeException because in one of the columns there is only a value for some of the rows (ex. if the size is over 0.5 there is a value, but if it is 0.5 or under that column is blank). How can I do this? So far I have:

      public static List GetAllNames(){
      SqlDatabase db = DatabaseFactory.CreateDatabase() as SqlDatabase;
      DbCommand cmd = db.GetStoredProcedureCommand("dbo.GetAllNames");

      List namelist= new List();

      using (IDataReader reader = db.ExecuteReader(cmd))
      {
      while(reader.Read())
      {
      namelist.Add(GetNameFromReader(reader));
      }
      }
      return namelist;
      }

      private static Name GetNameFromReader(IDataReader reader)
      {
      Name name_obj = new Name(
      (int)reader["ID"],
      ((reader)["size"] !=DBNull.Value)? (int)reader["size"] : (int)0));
      return name_obj;
      }

      Thanks so much for your help!

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      The code looks OK. I don't see why you'd get IndexOutOfRangeException for some rows but not others.

      1 Reply Last reply
      0
      • T Terick

        Hi, I am using an IDataReader with SQL and ASP.Net. I keep getting an IndexOutOfRangeException because in one of the columns there is only a value for some of the rows (ex. if the size is over 0.5 there is a value, but if it is 0.5 or under that column is blank). How can I do this? So far I have:

        public static List GetAllNames(){
        SqlDatabase db = DatabaseFactory.CreateDatabase() as SqlDatabase;
        DbCommand cmd = db.GetStoredProcedureCommand("dbo.GetAllNames");

        List namelist= new List();

        using (IDataReader reader = db.ExecuteReader(cmd))
        {
        while(reader.Read())
        {
        namelist.Add(GetNameFromReader(reader));
        }
        }
        return namelist;
        }

        private static Name GetNameFromReader(IDataReader reader)
        {
        Name name_obj = new Name(
        (int)reader["ID"],
        ((reader)["size"] !=DBNull.Value)? (int)reader["size"] : (int)0));
        return name_obj;
        }

        Thanks so much for your help!

        V Offline
        V Offline
        vaghelabhavesh
        wrote on last edited by
        #3

        Terick wrote:

        (reader)["size"]

        I am not getting this? By the way try this:

        private static Name GetNameFromReader(IDataReader reader)
        {
        Name name_obj = new Name(
        (int)reader["ID"],
        (reader["size"] != DBNull.Value)? (int)reader["size"] : (int)0);

                return name\_obj;
            }
        

        Be careful, there is no Undo Button(Ctrl+Z) in life.

        T 1 Reply Last reply
        0
        • V vaghelabhavesh

          Terick wrote:

          (reader)["size"]

          I am not getting this? By the way try this:

          private static Name GetNameFromReader(IDataReader reader)
          {
          Name name_obj = new Name(
          (int)reader["ID"],
          (reader["size"] != DBNull.Value)? (int)reader["size"] : (int)0);

                  return name\_obj;
              }
          

          Be careful, there is no Undo Button(Ctrl+Z) in life.

          T Offline
          T Offline
          Terick
          wrote on last edited by
          #4

          [Message Deleted]

          V 1 Reply Last reply
          0
          • T Terick

            [Message Deleted]

            V Offline
            V Offline
            vaghelabhavesh
            wrote on last edited by
            #5

            Terick wrote:

            otherwise the cell will be empty

            Hmm.... Now i get it. string.IsNullOrEmpty(reader["size"]) ? 0 : (int)reader["size"]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups