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. NullObjectRef in DataRow

NullObjectRef in DataRow

Scheduled Pinned Locked Moved C#
helpquestion
6 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.
  • A Offline
    A Offline
    A Wegierski
    wrote on last edited by
    #1

    I use a small table (1..5 records) with big number of fields (about 400 doubles with default 0). All work ok but some writes (about 30) into the columns generates Null Object Reference. (Windows 2000 5.00.2195 srvpack 3, Framework 1.0 srvpack 2) My code: ... loading data ... MyTable.AcceptChanges(); foreach(DataRow dr in MyTable.Rows) { foreach(string fieldname in MySomeColumnsStringArray) { ... some computing ... double x=Result(...); x+=Convert.ToDouble(dr[fieldname]); // works ok, x is ok try { dr[fieldname]=x; // **sometimes** generates Null Object Reference } catch(... } } What's wrong? Help, please. Sincerely AW

    A J 2 Replies Last reply
    0
    • A A Wegierski

      I use a small table (1..5 records) with big number of fields (about 400 doubles with default 0). All work ok but some writes (about 30) into the columns generates Null Object Reference. (Windows 2000 5.00.2195 srvpack 3, Framework 1.0 srvpack 2) My code: ... loading data ... MyTable.AcceptChanges(); foreach(DataRow dr in MyTable.Rows) { foreach(string fieldname in MySomeColumnsStringArray) { ... some computing ... double x=Result(...); x+=Convert.ToDouble(dr[fieldname]); // works ok, x is ok try { dr[fieldname]=x; // **sometimes** generates Null Object Reference } catch(... } } What's wrong? Help, please. Sincerely AW

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      What is the value of x when your code fails?

      A 2 Replies Last reply
      0
      • A andyharman

        What is the value of x when your code fails?

        A Offline
        A Offline
        A Wegierski
        wrote on last edited by
        #3

        Unfortunately, any: 0 10 105.91 NaN (I won't to write it in real application) Sincerely AW

        1 Reply Last reply
        0
        • A andyharman

          What is the value of x when your code fails?

          A Offline
          A Offline
          A Wegierski
          wrote on last edited by
          #4

          DataRow[idx] fails as well DataRow.ItemArray[idx] works :laugh: Sincerely AW

          1 Reply Last reply
          0
          • A A Wegierski

            I use a small table (1..5 records) with big number of fields (about 400 doubles with default 0). All work ok but some writes (about 30) into the columns generates Null Object Reference. (Windows 2000 5.00.2195 srvpack 3, Framework 1.0 srvpack 2) My code: ... loading data ... MyTable.AcceptChanges(); foreach(DataRow dr in MyTable.Rows) { foreach(string fieldname in MySomeColumnsStringArray) { ... some computing ... double x=Result(...); x+=Convert.ToDouble(dr[fieldname]); // works ok, x is ok try { dr[fieldname]=x; // **sometimes** generates Null Object Reference } catch(... } } What's wrong? Help, please. Sincerely AW

            J Offline
            J Offline
            Jim Stewart
            wrote on last edited by
            #5

            Could it be that the data in "fieldname" could be null? If so, you could try this:

            protected object getField(string FieldName)
            {
            if (m_row != null)
            {
            object retval = m_row[FieldName];
            if (retval != null && retval != System.DBNull.Value)
            {
            return retval;
            }
            else
            {
            return /* default value for data type e.g. 0 */;
            }
            }
            else
            {
            return null;
            }
            }

            Jim Stewart

            A 1 Reply Last reply
            0
            • J Jim Stewart

              Could it be that the data in "fieldname" could be null? If so, you could try this:

              protected object getField(string FieldName)
              {
              if (m_row != null)
              {
              object retval = m_row[FieldName];
              if (retval != null && retval != System.DBNull.Value)
              {
              return retval;
              }
              else
              {
              return /* default value for data type e.g. 0 */;
              }
              }
              else
              {
              return null;
              }
              }

              Jim Stewart

              A Offline
              A Offline
              A Wegierski
              wrote on last edited by
              #6

              My data couldn't be null. Thanks Hi, AW

              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