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. Trouble updating DataGridView.Datasource in a different class - Error "An object reference is required for the non-static field, method, or property"

Trouble updating DataGridView.Datasource in a different class - Error "An object reference is required for the non-static field, method, or property"

Scheduled Pinned Locked Moved C#
csharpdatabasetoolshelp
6 Posts 4 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
    astroudjr
    wrote on last edited by
    #1

    this is my first C# project so bear with me. Here is the code. HistoryDataView is part of Form1 and is set to public.

    namespace LockOut
    {
    public partial class Utility : Form
    {
    public Form1 m_parent;
    public Utility(Form1 frm1)
    {
    m_parent = frm1;
    }
    public static DataTable DeviceHistory()
    {
    OleDbConnection pcDataConn = DBConnection();
    try
    {
    DataSet ds = new DataSet();
    string SQL = "SQL Statement";
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    adapter.SelectCommand = new OleDbCommand(SQL, pcDataConn);
    adapter.Fill(ds);
    DataTable dt = new DataTable();
    dt = ds.Tables[0];
    if (dt.Rows.Count < 1)
    {
    dt = null;
    }
    pcDataConn.Close();
    return dt;
    }
    catch(Exception ex)
    {
    pcDataConn.Close();
    DataTable dt = null;
    Console.WriteLine(ex.Message.ToString());
    return dt;

            }
        }
        public static void UpdateDeviceHistory()
        {
            **m\_parent.HistoryDataView.DataSource** = DeviceHistory(); //<-- Here is where the error is generated
        }
    

    }

    namespace LockOut
    {
    public partial class Form1 : Form
    {
    private void Refresh_Click(object sender, EventArgs e)
    {
    Utility.UpdateDeviceHistory();
    }
    }
    }

    F H V 3 Replies Last reply
    0
    • A astroudjr

      this is my first C# project so bear with me. Here is the code. HistoryDataView is part of Form1 and is set to public.

      namespace LockOut
      {
      public partial class Utility : Form
      {
      public Form1 m_parent;
      public Utility(Form1 frm1)
      {
      m_parent = frm1;
      }
      public static DataTable DeviceHistory()
      {
      OleDbConnection pcDataConn = DBConnection();
      try
      {
      DataSet ds = new DataSet();
      string SQL = "SQL Statement";
      OleDbDataAdapter adapter = new OleDbDataAdapter();
      adapter.SelectCommand = new OleDbCommand(SQL, pcDataConn);
      adapter.Fill(ds);
      DataTable dt = new DataTable();
      dt = ds.Tables[0];
      if (dt.Rows.Count < 1)
      {
      dt = null;
      }
      pcDataConn.Close();
      return dt;
      }
      catch(Exception ex)
      {
      pcDataConn.Close();
      DataTable dt = null;
      Console.WriteLine(ex.Message.ToString());
      return dt;

              }
          }
          public static void UpdateDeviceHistory()
          {
              **m\_parent.HistoryDataView.DataSource** = DeviceHistory(); //<-- Here is where the error is generated
          }
      

      }

      namespace LockOut
      {
      public partial class Form1 : Form
      {
      private void Refresh_Click(object sender, EventArgs e)
      {
      Utility.UpdateDeviceHistory();
      }
      }
      }

      F Offline
      F Offline
      Fayu
      wrote on last edited by
      #2

      Any reason the Utility class is a form? I would make Utility a regular class and remove the UpdateDeviceHistory method. Then in Form1 i would do this:

      HistoryDataView.DataSource = Utility.DeviceHistory();

      A 1 Reply Last reply
      0
      • A astroudjr

        this is my first C# project so bear with me. Here is the code. HistoryDataView is part of Form1 and is set to public.

        namespace LockOut
        {
        public partial class Utility : Form
        {
        public Form1 m_parent;
        public Utility(Form1 frm1)
        {
        m_parent = frm1;
        }
        public static DataTable DeviceHistory()
        {
        OleDbConnection pcDataConn = DBConnection();
        try
        {
        DataSet ds = new DataSet();
        string SQL = "SQL Statement";
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        adapter.SelectCommand = new OleDbCommand(SQL, pcDataConn);
        adapter.Fill(ds);
        DataTable dt = new DataTable();
        dt = ds.Tables[0];
        if (dt.Rows.Count < 1)
        {
        dt = null;
        }
        pcDataConn.Close();
        return dt;
        }
        catch(Exception ex)
        {
        pcDataConn.Close();
        DataTable dt = null;
        Console.WriteLine(ex.Message.ToString());
        return dt;

                }
            }
            public static void UpdateDeviceHistory()
            {
                **m\_parent.HistoryDataView.DataSource** = DeviceHistory(); //<-- Here is where the error is generated
            }
        

        }

        namespace LockOut
        {
        public partial class Form1 : Form
        {
        private void Refresh_Click(object sender, EventArgs e)
        {
        Utility.UpdateDeviceHistory();
        }
        }
        }

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #3

        Why does Utility inherit from Form? If it really is a 'Utility' class, does it need to inherit from Form? In order to give the most appropriate answer, it would help to know this. In general the problem is that your methods are class methods (static), but your fields are not. Non-static items can only be accessed through an instance of a class, not through the class itself. [Mod] It took me so long to type this that your question has already been answered. [/Mod]

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        1 Reply Last reply
        0
        • A astroudjr

          this is my first C# project so bear with me. Here is the code. HistoryDataView is part of Form1 and is set to public.

          namespace LockOut
          {
          public partial class Utility : Form
          {
          public Form1 m_parent;
          public Utility(Form1 frm1)
          {
          m_parent = frm1;
          }
          public static DataTable DeviceHistory()
          {
          OleDbConnection pcDataConn = DBConnection();
          try
          {
          DataSet ds = new DataSet();
          string SQL = "SQL Statement";
          OleDbDataAdapter adapter = new OleDbDataAdapter();
          adapter.SelectCommand = new OleDbCommand(SQL, pcDataConn);
          adapter.Fill(ds);
          DataTable dt = new DataTable();
          dt = ds.Tables[0];
          if (dt.Rows.Count < 1)
          {
          dt = null;
          }
          pcDataConn.Close();
          return dt;
          }
          catch(Exception ex)
          {
          pcDataConn.Close();
          DataTable dt = null;
          Console.WriteLine(ex.Message.ToString());
          return dt;

                  }
              }
              public static void UpdateDeviceHistory()
              {
                  **m\_parent.HistoryDataView.DataSource** = DeviceHistory(); //<-- Here is where the error is generated
              }
          

          }

          namespace LockOut
          {
          public partial class Form1 : Form
          {
          private void Refresh_Click(object sender, EventArgs e)
          {
          Utility.UpdateDeviceHistory();
          }
          }
          }

          V Offline
          V Offline
          Vitaliy Tsvayer
          wrote on last edited by
          #4

          Your UpdateDeviceHistory method is static. But you are trying to access m_parent. You can't do that. Because m_parent is not static. To be more specific context of your program is needed but you could do this at least: 1. Remove static from your UpdateDeviceHistory method. 2. create Utility class instance and call UpdateDeviceHistory on that instance, that is: Utility utility = new Utility(some_form_with_HistoryDataView) utility.UpdateDeviceHistory() Good Luck

          Vitaliy Tsvayer Tikle

          1 Reply Last reply
          0
          • F Fayu

            Any reason the Utility class is a form? I would make Utility a regular class and remove the UpdateDeviceHistory method. Then in Form1 i would do this:

            HistoryDataView.DataSource = Utility.DeviceHistory();

            A Offline
            A Offline
            astroudjr
            wrote on last edited by
            #5

            That is actually how I had it running initially within Form1. But I have another method within the Utility Class That logs the transactions and I wanted to be able to just update the datagridview after every log, so I would still need to use it from this class. I just turned it into a button click for testing. I hope this makes sense.

            F 1 Reply Last reply
            0
            • A astroudjr

              That is actually how I had it running initially within Form1. But I have another method within the Utility Class That logs the transactions and I wanted to be able to just update the datagridview after every log, so I would still need to use it from this class. I just turned it into a button click for testing. I hope this makes sense.

              F Offline
              F Offline
              Fayu
              wrote on last edited by
              #6

              Not a very good idea to do that. But if thats what you want to do, you would have to make the m_parent static.

              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