0

I have two tables Activities and Sources. I want to check if IdSource from Sources, which is primary key, is found in the column Sources from table Activities. I did something like

private int nrSource {get;set;}
nrSource=(int)activitiesTableAdapter.ScalarQuery();
if (nrSource>0)
{
MessageBox.Show();
}

And the ScalarQuery method is something like select count (*) from Activities where exists (select * from Sources where Sources.IdSource=Activities.Sources).

I get an error where I make the cast: Object reference not set to an instance of an object. What am I doing wrong?

  • Please show the full error message, in the details of the error message you will see what object is being referenced, that is not initialized – Bernd Linde Mar 17 '15 at 10:51

1 Answers1

0

In the first place I would change your query to something like this:

select count (*) 
from Activities a
where exists (select * from Sources s where s.IdSource= a.Sources)

And if Activities.Sources is nullable and is a foreign key to Sources.IdSource, you could simply use:

select count (*) 
from Activities a
where a.Sources IS NOT NULL
Bruniasty
  • 418
  • 5
  • 18