473,321 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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(object 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_Distance,
a.Activity_Duration,
a.Activity_Route
};

dgvList.DataSource = act_types; // DataGridView
}

This model appears identical to many examples I've seen so what am I missing?
Oct 15 '08 #1
4 1134
Paolo <Pa***@discussions.microsoft.comwrote:
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(object 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_Distance,
a.Activity_Duration,
a.Activity_Route
};

dgvList.DataSource = 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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 15 '08 #2
Jon: this is my code so far:

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

public frmMain()
{
InitializeComponent();

string cnStr = <connection string// actual string snipped

DBDataContext db = new DBDataContext(cnStr);
}

private void btnList_Click(object 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_Distance,
a.Activity_Duration,
a.Activity_Route
};
dgvList.DataSource = act_types;
}
}

The query worked perfectly when it was part of frmMain
"Jon Skeet [C# MVP]" wrote:
Paolo <Pa***@discussions.microsoft.comwrote:
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(object 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_Distance,
a.Activity_Duration,
a.Activity_Route
};

dgvList.DataSource = 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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 16 '08 #3
Paolo <Pa***@discussions.microsoft.comwrote:
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(cnStr);

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

db = new DBDataContext(cnStr);

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 16 '08 #4
Jon: thanks for pointing that out.

"Jon Skeet [C# MVP]" wrote:
Paolo <Pa***@discussions.microsoft.comwrote:
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(cnStr);

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

db = new DBDataContext(cnStr);

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 16 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
5
by: Tony Wright | last post by:
Hi, I am having a problem installing an msi for a web site. The error message I am getting is: "The specified path 'http://mipdev05/features/Fas2' is unavailable. The Internet Information...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
1
by: Bram | last post by:
I'm writing an application using LinqToSQL. Basically the application works as follows: - Read a file - Add rows to the database (the rows belong to more than one table). - Call...
0
by: d-42 | last post by:
You wrote a great deal and there is so much I would have liked to elaborate on but this really de-railed the whole debate. You posted that linq DOES throw an exception if its not ordered, and that...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
1
by: =?Utf-8?B?S2ltIFM=?= | last post by:
In my professional work I work with SQL server express and VS 2008 Pro and re I know how to connect LinqtoSql. But then I work on my own computer with SQL server express and VS 2008 express its...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.