Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Now i modified the code as: .FirstorDefault(); to ToList();

Now I got :
Quote:
System.NullReferenceException: Object reference not set to an instance of an object.



rptStudyCertificate rsc = new rptStudyCertificate();
using (DataClasseDataContext db = new DataClasseDataContext())
{
var list = (from data in db.StudyCertificates
join clsdet in db.StudentClassDetails on data.AdmissionNo equals clsdet.AdmissionNo
where data.ID == Convert.ToInt16(sno)
select new { ID = data.ID, Class = clsdet.Class, AcademicYear = clsdet.AcademicYear, AdmissionNo = clsdet.AdmissionNo, Conduct = data.Conduct}).ToList();


if (list != null)
{
rsc.SetDataSource(list);
CrystalReportViewer1.ReportSource = rsc;
}
}



Thanks in advance...................
Posted
Updated 12-Jun-13 21:16pm
v5
Comments
Jameel VM 11-Jun-13 6:54am    
in which line you got the exception?
Siva Prakash Nammi 11-Jun-13 7:08am    
CrystalReportViewer1.ReportSource=rsc;
Siva Prakash Nammi 11-Jun-13 7:07am    
CrystalReportViewer1.ReportSource=rsc;
Ahmed Bensaid 11-Jun-13 16:44pm    
An object is necessarily null on this line.
And the winner is ... CrystalReportViewer1 ;)
Where do you declare and instantiate this object ?
Siva Prakash Nammi 12-Jun-13 0:32am    
Actually i got values into the list, but it shows an error like

System.NullReferenceException: Object reference not set to an instance of an object.


Please help me...

1 solution

Try this:

C#
rptStudyCertificate rsc = new rptStudyCertificate();

//This is the instantiation that Ahmed suggested.  You can do this here or earlier
CrystalReportViewer1 = new CrystalReportViewer();

using (DataClasseDataContext db = new DataClasseDataContext())
{
   var list = (from data in db.StudyCertificates
   join clsdet in db.StudentClassDetails on data.AdmissionNo equals clsdet.AdmissionNo
   where data.ID == Convert.ToInt16(sno)
   select new { ID = data.ID, Class = clsdet.Class, AcademicYear = 
   clsdet.AcademicYear, AdmissionNo = 
   clsdet.AdmissionNo, Conduct = data.Conduct}).ToList();
 

  if (list != null)
  {
    rsc.SetDataSource(list);
    CrystalReportViewer1.ReportSource = rsc;
  }
}
 
Share this answer
 
Comments
Siva Prakash Nammi 13-Jun-13 3:14am    
i cant solve this error in crystal reports with linq to sql :


ERROR: "DataSet does not support System.Nullable<>".
I got this error in. "rsc.SetDataSource(data);"


Below is my code:


using (DataClasseDataContext db = new DataClasseDataContext())
{
var data = (from records in db.vwStudycertificates

where records.ID == Convert.ToInt16(sno)
select new { ID = records.ID, Class = records.Class, AcademicYear = records.AcademicYear, AdmissionNo = records.AdmissionNo, Conduct = records.Conduct, Name = records.Name, surname = records.Surname, SchoolID = records.SchoolID }).ToList();
if (data != null)
{

rptStudyCertificate rsc = new rptStudyCertificate();
rsc.SetDataSource(data);
CrystalReportViewer1.ReportSource = rsc;

}


Thanks in advance.........................
Andy Lanng 13-Jun-13 4:35am    
Ok - this is a new question so should be posted as such. You'll get better responses and we get more points ^_^
Your var is an anonymous type which has a nullable value because one of you result fields is null. It cannot translate a nullable object into a datatable which is what SetDataSource is attempting to do.
You might find implicitly converting 'data' to a datatable first so you can control the conversion. take a look at this extension method that does just that
http://stackoverflow.com/questions/1253725/convert-ienumerable-to-datatable

PS: please accept the solution to the previous question. Thanks ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900