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. Web Development
  3. ASP.NET
  4. Handling a null value

Handling a null value

Scheduled Pinned Locked Moved ASP.NET
databasehelpquestion
4 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.
  • E Offline
    E Offline
    Ekjon
    wrote on last edited by
    #1

    I have a query suppossed to return average values (SELECT AVG(DATA_VALUE)....). However, it returns null when no data is present for the matching criteria. I am using a data reader to read the value. like: dr = cmdSelect.ExecuteReader(); if (dr.HasRows) { dr.Read(); string getAvg = dr.GetString(0); if (getAvg != null) { valArr[0] = Convert.ToDouble(getAvg); log.Add(...); } else{...} } Funny thing is - dr.HasRows is true because the query is returning a record even though the value is null. I also tried GetObject(0) instead of GetString(0), and tried cmd.ExecuteScalar() instead of ExecuteReader(), but still could not manage it. Its going to an Exception saying - "Data is Null. This method or property cannot be called on Null values." Any help? Thanks.

    Y P 2 Replies Last reply
    0
    • E Ekjon

      I have a query suppossed to return average values (SELECT AVG(DATA_VALUE)....). However, it returns null when no data is present for the matching criteria. I am using a data reader to read the value. like: dr = cmdSelect.ExecuteReader(); if (dr.HasRows) { dr.Read(); string getAvg = dr.GetString(0); if (getAvg != null) { valArr[0] = Convert.ToDouble(getAvg); log.Add(...); } else{...} } Funny thing is - dr.HasRows is true because the query is returning a record even though the value is null. I also tried GetObject(0) instead of GetString(0), and tried cmd.ExecuteScalar() instead of ExecuteReader(), but still could not manage it. Its going to an Exception saying - "Data is Null. This method or property cannot be called on Null values." Any help? Thanks.

      Y Offline
      Y Offline
      Yusuf
      wrote on last edited by
      #2

      all you need is DBNull[^]class if (! DBNull.Value.Equals(dr[fieldName])) { //do somthing }

      ɟnsnʎ ʎlʇuǝƃıllǝʇuı uoıʇsǝnb ɐ ʞsɐ oʇ ʍoɥ [^]

      1 Reply Last reply
      0
      • E Ekjon

        I have a query suppossed to return average values (SELECT AVG(DATA_VALUE)....). However, it returns null when no data is present for the matching criteria. I am using a data reader to read the value. like: dr = cmdSelect.ExecuteReader(); if (dr.HasRows) { dr.Read(); string getAvg = dr.GetString(0); if (getAvg != null) { valArr[0] = Convert.ToDouble(getAvg); log.Add(...); } else{...} } Funny thing is - dr.HasRows is true because the query is returning a record even though the value is null. I also tried GetObject(0) instead of GetString(0), and tried cmd.ExecuteScalar() instead of ExecuteReader(), but still could not manage it. Its going to an Exception saying - "Data is Null. This method or property cannot be called on Null values." Any help? Thanks.

        P Offline
        P Offline
        Perry Holman
        wrote on last edited by
        #3

        You can handle that as below: if (dr["fieldname"] != null) { //do something }

        Welcome to www.softwaretree.net! You can find many excellent audio/video converter tools there!

        E 1 Reply Last reply
        0
        • P Perry Holman

          You can handle that as below: if (dr["fieldname"] != null) { //do something }

          Welcome to www.softwaretree.net! You can find many excellent audio/video converter tools there!

          E Offline
          E Offline
          Ekjon
          wrote on last edited by
          #4

          Thanks to both of you.

          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