LINQtoSQL error??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?UGFvbG8=?=

    LINQtoSQL error??

    I'm getting an error "Object reference not set to an instance of an object."
    with line number shown as <### as i try to popuate a DataGridView:

    private void btnList_Click(o bject sender, EventArgs e)
    {
    var act_types = <###
    from a in db.Activities
    from t in db.Types
    where a.Activity_Type == t.Type_Id

    orderby a.Activity_Date
    select new
    {
    a.Activity_Date ,
    t.Type_Desc,
    a.Activity_Dist ance,
    a.Activity_Dura tion,
    a.Activity_Rout e
    };

    dgvList.DataSou rce = act_types; // DataGridView
    }

    This model appears identical to many examples I've seen so what am I missing?
  • Jon Skeet [C# MVP]

    #2
    Re: LINQtoSQL error??

    Paolo <Paolo@discussi ons.microsoft.c omwrote:
    I'm getting an error "Object reference not set to an instance of an object."
    with line number shown as <### as i try to popuate a DataGridView:
    >
    private void btnList_Click(o bject sender, EventArgs e)
    {
    var act_types = <###
    from a in db.Activities
    from t in db.Types
    where a.Activity_Type == t.Type_Id
    >
    orderby a.Activity_Date
    select new
    {
    a.Activity_Date ,
    t.Type_Desc,
    a.Activity_Dist ance,
    a.Activity_Dura tion,
    a.Activity_Rout e
    };
    >
    dgvList.DataSou rce = act_types; // DataGridView
    }
    >
    This model appears identical to many examples I've seen so what am I missing?
    Where are you getting db from, and is it definitely non-null?

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • =?Utf-8?B?UGFvbG8=?=

      #3
      Re: LINQtoSQL error??

      Jon: this is my code so far:

      public partial class frmMain : Form
      {
      DBDataContext db = null; // DBDataContext defined in DBDataContext.c s

      public frmMain()
      {
      InitializeCompo nent();

      string cnStr = <connection string// actual string snipped

      DBDataContext db = new DBDataContext(c nStr);
      }

      private void btnList_Click(o bject sender, EventArgs e)
      {
      var act_types =
      from a in db.Activities
      from t in db.Types
      where a.Activity_Type == t.Type_Id

      orderby a.Activity_Date
      select new
      {
      a.Activity_Date ,
      t.Type_Desc,
      a.Activity_Dist ance,
      a.Activity_Dura tion,
      a.Activity_Rout e
      };
      dgvList.DataSou rce = act_types;
      }
      }

      The query worked perfectly when it was part of frmMain
      "Jon Skeet [C# MVP]" wrote:
      Paolo <Paolo@discussi ons.microsoft.c omwrote:
      I'm getting an error "Object reference not set to an instance of an object."
      with line number shown as <### as i try to popuate a DataGridView:

      private void btnList_Click(o bject sender, EventArgs e)
      {
      var act_types = <###
      from a in db.Activities
      from t in db.Types
      where a.Activity_Type == t.Type_Id

      orderby a.Activity_Date
      select new
      {
      a.Activity_Date ,
      t.Type_Desc,
      a.Activity_Dist ance,
      a.Activity_Dura tion,
      a.Activity_Rout e
      };

      dgvList.DataSou rce = act_types; // DataGridView
      }

      This model appears identical to many examples I've seen so what am I missing?
      >
      Where are you getting db from, and is it definitely non-null?
      >
      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: LINQtoSQL error??

        Paolo <Paolo@discussi ons.microsoft.c omwrote:
        Jon: this is my code so far:
        <snip>
        The query worked perfectly when it was part of frmMain
        Yes, it would. Look at this line of your constructor:

        DBDataContext db = new DBDataContext(c nStr);

        That's declaring a new *local variable* called db - it's not setting
        the instance variable. Change it to this:

        db = new DBDataContext(c nStr);

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • =?Utf-8?B?UGFvbG8=?=

          #5
          Re: LINQtoSQL error??

          Jon: thanks for pointing that out.

          "Jon Skeet [C# MVP]" wrote:
          Paolo <Paolo@discussi ons.microsoft.c omwrote:
          Jon: this is my code so far:
          >
          <snip>
          >
          The query worked perfectly when it was part of frmMain
          >
          Yes, it would. Look at this line of your constructor:
          >
          DBDataContext db = new DBDataContext(c nStr);
          >
          That's declaring a new *local variable* called db - it's not setting
          the instance variable. Change it to this:
          >
          db = new DBDataContext(c nStr);
          >
          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com
          >

          Comment

          Working...