0

I'm using C#, WPF and Microsoft SQL Server.

When I press my Export button, it results in errors. The header is populated, but not the data.

I'm suspicious with my filter function.

Here's the filter function which correspond to the DataGrid.

private void theFilter(string FilterValue, string FilterValueYear, string FilterValueMonth) 
{
    string thisQuery = "SELECT [TransportRecord].[TransportRecordId], [Transporter].[Name] as [TransporterName], [Customer].[Name] as [CustomerName], [Destination].[Name] as [DestinationName], [FuelType].[Name] as [FuelTypeName], [TransportRecord].[SO], [TransportRecord].[Quantity],  [TransportRecord].[Price], [TransportRecord].[Date]" +
            "               FROM [TransportRecord]" +
            "               INNER JOIN [Transporter] ON [TransportRecord].[TransporterId] = [Transporter].[TransporterId]" +
            "               INNER JOIN [Customer] ON [TransportRecord].[CustomerId] = [Customer].[CustomerId]" +
            "               INNER JOIN [Destination] ON [TransportRecord].[DestinationId] = [Destination].[DestinationId]" +
            "               INNER JOIN [FuelType] ON [TransportRecord].[FuelTypeId] = [FuelType].[FuelTypeId]" +
            "               WHERE CONCAT([Transporter].[Name], [Customer].[Name], [FuelType].[Name], [Destination].[Name], [TransportRecord].[SO], [TransportRecord].[Quantity], [TransportRecord].[Price]) LIKE @FilterValue AND DATEPART(YYYY, [TransportRecord].[Date]) LIKE @FilterValueYear AND DATEPART(MM, [TransportRecord].[Date]) LIKE @FilterValueMonth AND DATEPART(DD, [TransportRecord].[Date]) BETWEEN @FilterValueStartPeriod AND @FilterValueEndPeriod ORDER BY [TransportRecord].[Date]";

    using(SqlConnection thisSqlConnection = new SqlConnection(theConnectionString))
    using(SqlCommand thisSqlCommand = new SqlCommand(thisQuery, thisSqlConnection)) 
    {
        thisSqlCommand.Parameters.Add("@FilterValue", SqlDbType.Text).Value = "%" + FilterValue + "%";
        thisSqlCommand.Parameters.Add("@FilterValueYear", SqlDbType.Text).Value = "%" + FilterValueYear + "%";
        thisSqlCommand.Parameters.Add("@FilterValueMonth", SqlDbType.Text).Value = "%" + FilterValueMonth + "%";
        thisSqlCommand.Parameters.Add("@FilterValueStartPeriod", SqlDbType.VarChar).Value = theStartPeriod;
        thisSqlCommand.Parameters.Add("@FilterValueEndPeriod", SqlDbType.VarChar).Value = theEndPeriod;

        using(SqlDataAdapter thisSqlDataAdapter = new SqlDataAdapter(thisSqlCommand))
        using(DataTable thisDataTable = new DataTable()) 
        {
            thisSqlDataAdapter.Fill(thisDataTable);

            DataGrid_TransportRecord.ItemsSource = thisDataTable.DefaultView;
        }
    }
}

And here's my export function:

private void Button_ExcelExport_Click(object sender, RoutedEventArgs e) 
{
    theExcelExport();
}

private void theExcelExport() 
{
    Excel.Application thisExcelApplication = new Excel.Application();
    thisExcelApplication.Visible = true;

    Excel.Workbook thisExcelWorkbook = thisExcelApplication.Workbooks.Add(System.Reflection.Missing.Value);

    Excel.Worksheet thisExcelWorksheet = (Excel.Worksheet)thisExcelWorkbook.Sheets[1];

    for(int j = 0; j < DataGrid_TransportRecord.Columns.Count; j ++) 
    {
         Excel.Range thisExcelRange = (Excel.Range)thisExcelWorksheet.Cells[1, j + 1];
         thisExcelWorksheet.Cells[1, j + 1].Font.Bold = true;
         thisExcelWorksheet.Columns[j + 1].ColumnWidth = 15;
         thisExcelRange.Value2 = DataGrid_TransportRecord.Columns[j].Header;
    }

    for(int i = 0; i < DataGrid_TransportRecord.Columns.Count; i++) 
    {
        for(int j = 0; j < DataGrid_TransportRecord.Items.Count; j ++) 
        {
            TextBlock thisTextBlock = DataGrid_TransportRecord.Columns[i].GetCellContent(DataGrid_TransportRecord.Items[j]) as TextBlock;
            Microsoft.Office.Interop.Excel.Range thisExcelRange = (Microsoft.Office.Interop.Excel.Range)thisExcelWorksheet.Cells[j + 2, i + 1];
            thisExcelRange.Value2 = thisTextBlock.Text;
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lem Bidi
  • 63
  • 9

0 Answers0