2

i have an app which has btn to preview report made in crystal report. I added Dataset as datasource of the report and dragged datatable from the toolbox and added the fields I need as columns. I got the instruction from this link http://aspalliance.com/2049_Use_LINQ_to_Retrieve_Data_for_Your_Crystal_Reports.2. This is my 2nd report the first one works and did not encounter any prob at all that is why i am confused, not to mention it also has nullable column. the error says: DataSet does not support System.Nullable<>.

  private void ShowReportView()
    {

        string reportFile = "JudgeInfoFMReport.rpt";
        ObservableCollection<tblJudgeFileMaint> judgeFileMaintList;

        judgeFileMaintList = GenerateReport();

        if (judgeFileMaintList.Count > 0)
        {
            CrystalReportViewerUC crview2 = new CrystalReportViewerUC();
            crview2.SetReportPathFile(reportFile, judgeFileMaintList);
            crview2.ShowDialog();
        }
        else
        {
            System.Windows.MessageBox.Show("No record found.", module, MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }

private ObservableCollection<tblJudgeFileMaint> GenerateReport()
    {
        var result = FileMaintenanceBusiness.Instance.GetAllJudgeInfoList();
        return new ObservableCollection<tblJudgeFileMaint>(result);
    }

The error is in the part where I set datasource report.SetDataSource

 public bool SetReportPathFile(string reportPathFile, IEnumerable enumerable)
    {

            string reportFolder = @"\CrystalReportViewer\Reports\";
            string filename = System.Windows.Forms.Application.StartupPath + reportFolder + reportPathFile;  // "\\Reports\\CrystalReports\\DateWiseEmployeeInfoReport.rpt";
            ReportPathFile = filename;
            report.Load(ReportPathFile);
            report.SetDataSource(enumerable);
            report.SetDatabaseLogon("sa", "admin007");
            bRet = true;
       }

        _IsLoaded = bRet;

        return bRet;
    }

I read some answers and says I should set the null value to DBNUll which I did in the properties window of each column if it is nullable. Can anyone help me please? thanks

user742102
  • 1,335
  • 8
  • 32
  • 51

4 Answers4

4

Your question can be seen in this post, but in a generic way ... that way you can pass an Object to a DataSet typed!

.NET - Convert Generic Collection to DataTable

Community
  • 1
  • 1
0

I found little help from the other proposed answers but this solution worked. A different way to solve this problem is to make the data column nullable.

 DataColumn column = new DataColumn("column", Type.GetType("System.Int32"));
 column.AllowDBNull = true;
 dataTable.Columns.Add(column);

https://learn.microsoft.com/en-us/dotnet/api/system.data.datacolumn.allowdbnull?view=netcore-3.1

Dan Cundy
  • 2,649
  • 2
  • 38
  • 65
0

figured it out. by using a collectionextention, copied somewhere, I forgot the link. Os to whoever it is who made the class, credits to you.

class method looks like this.

public statis class CollectionExtension {
      public static DataSet ToDataSet<T>(this IEnumerable<T> collection, string dataTableName)
    {
        if (collection == null)
        {
            throw new ArgumentNullException("collection");
        }

        if (string.IsNullOrEmpty(dataTableName))
        {
            throw new ArgumentNullException("dataTableName");
        }

        DataSet data = new DataSet("NewDataSet");
        data.Tables.Add(FillDataTable(dataTableName, collection));
        return data;
    }
 }

then you can use it by doing this in getting your source to your report:

 private DataSet GenerateNeutralContEducReport(string dsName)
    {
        var contEduHistoryList = FileMaintenanceBusiness.Instance.GetManyNeutralFMContEducHistoryInfobyKeyword(CurrentNeutralFM.NeutralID, "NeutralID").ToList();
       return CollectionExtensions.ToDataSet<tblContinuingEducationHistory>(contEduHistoryList, dsName);
    }
user742102
  • 1,335
  • 8
  • 32
  • 51
  • 1
    That doesn't actually answer your question (`FillDataTable()` is missing), but this does: http://stackoverflow.com/questions/701223/net-convert-generic-collection-to-datatable – jrummell Jun 19 '12 at 15:26
-1
foreach (PropertyDescriptor property in properties)
{
    dt.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Abdullah SARGIN
  • 1,646
  • 1
  • 13
  • 19