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. Database not updating values - nullreference exception keeps getting thrown [modified]

Database not updating values - nullreference exception keeps getting thrown [modified]

Scheduled Pinned Locked Moved C#
databasehelpsql-serversysadmindata-structures
6 Posts 2 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
    Eagle32
    wrote on last edited by
    #1

    Hi, I am using SQL Server Compact 3.5 for my database. I am trying to update the data in the database table, dataset and the gridview by looping through each of the datagridview row by doing the following:

    for(int i = 0; i<AnimalsDataGridView.Rows.Count; i++)
    {
    try
    {
    if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))
    {
    //get the filePath
    String currentPath = AnimalsDataGridView.Rows[i].Cells[4].Value.ToString();
    Image img = Image.FromFile(currentPath);

      Image thumbnail = SetThumbnail(img, 200, 200);
    
      //get the title of the image and store in imageTitle variable
                        
       //save image to byte array
       byte\[\] imageInfo = ImageToByteArray(thumbnail);
                        
       //Primary key of the database. I have not enabled the autogeneration feature regarding this.
        Guid guid = getGuid(thumbnail);
    
       this.ZooTableAdapter.Update(guid, imageTitle, imageInfo, currentPath, guid);
       this.ZooTableAdapter.Fill(ZooDataSet.Animals);
    }
    

    }
    catch (Exception e)
    {
    Console.WriteLine(e.Message.ToString());
    }
    }

    The error occurs on the the following line:

    if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))

    This is the error:

    NullReference Exception was unhandled
    Object reference not set to an instance of an object.

    Is it because it is trying to update an empty row i.e the blank row? The Update query looks like this:

    UPDATE [Animals] SET [Id] = @p1, [Title] = @p2, [Thumbnail] = @p3, [FullPath] = @p4 WHERE (([Id] = @p5))

    If somebody could assist me assist me with this problem, i would greatly appreciate the help. Thanks in advance,

    modified on Friday, March 12, 2010 7:42 PM

    D 1 Reply Last reply
    0
    • E Eagle32

      Hi, I am using SQL Server Compact 3.5 for my database. I am trying to update the data in the database table, dataset and the gridview by looping through each of the datagridview row by doing the following:

      for(int i = 0; i<AnimalsDataGridView.Rows.Count; i++)
      {
      try
      {
      if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))
      {
      //get the filePath
      String currentPath = AnimalsDataGridView.Rows[i].Cells[4].Value.ToString();
      Image img = Image.FromFile(currentPath);

        Image thumbnail = SetThumbnail(img, 200, 200);
      
        //get the title of the image and store in imageTitle variable
                          
         //save image to byte array
         byte\[\] imageInfo = ImageToByteArray(thumbnail);
                          
         //Primary key of the database. I have not enabled the autogeneration feature regarding this.
          Guid guid = getGuid(thumbnail);
      
         this.ZooTableAdapter.Update(guid, imageTitle, imageInfo, currentPath, guid);
         this.ZooTableAdapter.Fill(ZooDataSet.Animals);
      }
      

      }
      catch (Exception e)
      {
      Console.WriteLine(e.Message.ToString());
      }
      }

      The error occurs on the the following line:

      if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))

      This is the error:

      NullReference Exception was unhandled
      Object reference not set to an instance of an object.

      Is it because it is trying to update an empty row i.e the blank row? The Update query looks like this:

      UPDATE [Animals] SET [Id] = @p1, [Title] = @p2, [Thumbnail] = @p3, [FullPath] = @p4 WHERE (([Id] = @p5))

      If somebody could assist me assist me with this problem, i would greatly appreciate the help. Thanks in advance,

      modified on Friday, March 12, 2010 7:42 PM

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      Eagle32 wrote:

      //get the filePath String currentPath = picturesDataGridView.Rows[i].Cells[4].Value.ToString();

      Yep that is it. The cell has no value => exception. I wolud force something like "Invalid Bitmap" in the DB. So if there is no file specified it(the program) will automatically put "Invalid Bitmap" and load/show a red X or something kind of image. To check for null is expensive if you have many rows.

      E 1 Reply Last reply
      0
      • D Dan Mos

        Eagle32 wrote:

        //get the filePath String currentPath = picturesDataGridView.Rows[i].Cells[4].Value.ToString();

        Yep that is it. The cell has no value => exception. I wolud force something like "Invalid Bitmap" in the DB. So if there is no file specified it(the program) will automatically put "Invalid Bitmap" and load/show a red X or something kind of image. To check for null is expensive if you have many rows.

        E Offline
        E Offline
        Eagle32
        wrote on last edited by
        #3

        Hi, I have already enabled the feature to show an invalid bitmap. The last row in the gridview does show an invalid bitmap. I am just checking if i can have access to the the actual filepath of the animal image. Any advice? Thanks,

        D 1 Reply Last reply
        0
        • E Eagle32

          Hi, I have already enabled the feature to show an invalid bitmap. The last row in the gridview does show an invalid bitmap. I am just checking if i can have access to the the actual filepath of the animal image. Any advice? Thanks,

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          well use a try ... cath. Even the File.Exists(...) will throw an exception if the current user doesn't have enough privileges. [Added] but in your initial post you said: The error occurs on the the following line:

          if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))

          This is the error: "NullReference Exception was unhandled. Object reference not set to an instance of an object." This has nothing to do with the access to the file. The null exception/stuff comes from your AnimalsDataGridView.Rows[i].Cells[4].Value.ToString() [/Added]

          E 1 Reply Last reply
          0
          • D Dan Mos

            well use a try ... cath. Even the File.Exists(...) will throw an exception if the current user doesn't have enough privileges. [Added] but in your initial post you said: The error occurs on the the following line:

            if (File.Exists(AnimalsDataGridView.Rows[i].Cells[4].Value.ToString()))

            This is the error: "NullReference Exception was unhandled. Object reference not set to an instance of an object." This has nothing to do with the access to the file. The null exception/stuff comes from your AnimalsDataGridView.Rows[i].Cells[4].Value.ToString() [/Added]

            E Offline
            E Offline
            Eagle32
            wrote on last edited by
            #5

            I discovered its because of the Blank row in the database. I did the following but its not actually updating any of the rows. By the way no exceptions are being thrown if i do this:

            foreach (DataGridViewRow row in AnimalDataGridView.Rows)
            {
            if (!row.IsNewRow)
            {
            //check if file exists
            {
            //do all the processing needed within here

                    //call the Update method of the adpater
                    //call the Fill method on the dataset
                   }
                }
            }
            //call update to update gridview
            

            }

            Can you advise?

            D 1 Reply Last reply
            0
            • E Eagle32

              I discovered its because of the Blank row in the database. I did the following but its not actually updating any of the rows. By the way no exceptions are being thrown if i do this:

              foreach (DataGridViewRow row in AnimalDataGridView.Rows)
              {
              if (!row.IsNewRow)
              {
              //check if file exists
              {
              //do all the processing needed within here

                      //call the Update method of the adpater
                      //call the Fill method on the dataset
                     }
                  }
              }
              //call update to update gridview
              

              }

              Can you advise?

              D Offline
              D Offline
              Dan Mos
              wrote on last edited by
              #6

              Don't allow null rows/fields. First Open Up Access and fill in with a default Empty or Default text the fields that are null. Now, lets say you have a table with 3 columns ID and AnimalName and AnimalPicture. When you/the user introduce some data if the AnimalPicture textbox is empty than put in something like Empty or Default. I really don't know how you introduce/get the datas so without much detail that is the best I could come up with.

              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