views:

64

answers:

1

I am using Crystal Report for creating a report. I have two tables, Driver_Specifications and WatchRow.

Primary key of WatchRow is foreign key of Driver_Specifications. I set some field in my report file to show the data of Driver_Specifications and I want to show a specific field of WatchRow instead of the foreign key of Driver_Specifications. What is the query of LINQ for this problem?

This is what I have so far:

var q = (from d in taxi_AgencyDataSet.Driver_Specifications
         orderby d.First_Name, d.ID_Driver
         select new
         {
             d.First_Name,
             d.Last_Name,
             d.Car_Name,
             d.Car_Color,
             d.WatchRow.Watch_Name,
             d.ID_Watch
         }).ToList();

I get the following error:

Object reference not set to an instance of an object
A: 

You need to check for null when accessing d.WatchRow to avoid throwing a NullReferenceException:

var q = (from d in taxi_AgencyDataSet.Driver_Specifications
         orderby d.First_Name, d.ID_Driver
         select new
         {
             d.First_Name,
             d.Last_Name,
             d.Car_Name,
             d.Car_Color,
             Watch_Name = d.WatchRow == null ? "None" : d.WatchRow.Watch_Name,
             d.ID_Watch
         }).ToList();
Mark Byers
Yeah that's right , I write exactly like this , but I set some field for table1 in report file and set a field from table2 for set of specefic field like your example( Baz field ) but a error apear .I dont know what shoul I do!!
danialtehrani
@user356760: What is the error? If you could include your code in the question, and the error message too, it will be easier to help you.
Mark Byers
This is a error that appear :Object reference not set to an instance of an object.and this is the code : var q =(from d in taxi_AgencyDataSet.Driver_Specificationsorderby d.First_Name, d.ID_Driver select new { d.First_Name, d.Last_Name, d.Car_Name, d.Car_Color, d.WatchRow.Watch_Name, d.ID_Watch }).ToList();Driver_Specifications is table1 and Whatch is table2
danialtehrani
@danialtehrani: See my updated answer.
Mark Byers
sorry , what is your mean ?
danialtehrani
@danialtehrani: I mean refresh the page so that you can see that I have changed my answer. I have also added the extra information from your comment to the question. You should put this information into the question in the first place in future, please.
Mark Byers
Yeah, I use your code but it dont let me compile it and I have this error :Error 1 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access. F:\Ali\C#\Taxi Servis\Taxi Servis\ReportFrm.cs 209 14 Taxi Servis
danialtehrani
@danialtehrani: Sorry there was an error in my code (I haven't tested it). I think it should be fixed now.
Mark Byers
Thank you, yeah it works .but now No data appear in my CrystalReportViewer control when I run the programm.
danialtehrani