This project has moved. For the latest updates, please go here.
1
Vote

An item with the same key has already been added

description

Trying to use Doddleproject for the first time with a Webmatrix Razor project. I have got the Library implemented and referenced ok in the code. However, when I run it the following error is displayed "An item with the same key has already been added."

The following is the code where it fails:
var report = new Report(data.ToReportSource(), new DoddleReport.OpenXml.ExcelReportWriter());
The preceding code to set the Query is:
var data = db.Query(SQLSELECT);
var grid = new WebGrid(source: data, rowsPerPage:100); 
var report = new Report(data.ToReportSource(), new DoddleReport.OpenXml.ExcelReportWriter());
using (var stream = File.Create("Report.xlsx")){
            report.WriteReport(stream);
        }
Process.Start("Report.xlsx");
I am unsure as to whether it is the Query it does not like or what it is.

I have looked through all of the documentation and can't find any example that does not use LINQ.

Any help or examples would be appreciated as I would really like to use this on my development.

comments

Harley63 wrote Sep 12, 2013 at 11:08 AM

Ok, solved it myself after sleeping on it.

Created my own function to take the SQL CE database Query and convert it to a DataTable for use as valid DoddleReport iReportSource.

So for anyone else using WebMatrix with Razor and wants to use DoddleReport, create the data variable as follows:
        var data = CreateDataTable(Server.MapPath("~/App_Data/" + dbName + ".sdf"), SQLSELECT);
Where 'CreateDataTable' is a function as follows:

@functions{
public static DataTable CreateDataTable(string sqlCeDb, string sqlCmd)
{
    DataSet dataSet = new DataSet();
    DataTable dt = new DataTable();

    try {
        SqlCeConnection sqlConn= new SqlCeConnection();
        sqlConn.ConnectionString = "Data Source = " + sqlCeDb; 

        SqlCeCommand cmd = new SqlCeCommand(); 
        cmd.CommandType = CommandType.Text; 
        cmd.CommandText = sqlCmd; 
        cmd.Connection = sqlConn;

        sqlConn.Open();
        SqlCeDataAdapter sda = new SqlCeDataAdapter(cmd);

        sda.Fill(dataSet);
        sqlConn.Close(); 

        dt = dataSet.Tables[0];
        return dt;
    }
    catch (Exception ex)
    {
        return dt;
    }      
}
}

You will also need to ensure you have an entry in your web.config file for the assembly to enable the connection in the function as follows:
<compilation targetFramework="4.0" debug="true">
  <assemblies>
    <add assembly="System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" />
  </assemblies>
</compilation>