0

I'm developing for the first time in layers (Data, Business, Presentation), before I was doing it directly, now I want to show a report, these are the codes:

//DATA
//================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;

namespace CapaDatos
{
    public class DCategorias
    {
        public int IDCategoria { get; set; }
        public string Categoria { get; set; }
        public string Notas { get; set; }
    
    public DataTable Mostrar()
        {
            DataTable TablaDatos = new DataTable("Categorias");
            using (OleDbConnection OleDbConexion = new OleDbConnection())
            {
                try
                {
                    OleDbConexion.ConnectionString = DConexion.ChronusBD;
                    OleDbConexion.Open();

                    OleDbCommand OleDbComando = new OleDbCommand
                    {
                        Connection = OleDbConexion,
                        CommandText = @"SELECT Categoria, Notas
                                    FROM Categorias
                                    ORDER BY Categoria",
                        CommandType = CommandType.Text
                    };

                    OleDbDataAdapter OleDbAdaptadorDatos = new OleDbDataAdapter(OleDbComando);
                    OleDbAdaptadorDatos.Fill(TablaDatos);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error al intentar ejecutar la instrucción. " + ex.Message, ex);
                }
                finally
                {
                    OleDbConexion.Close();
                }
            }

            return TablaDatos;
        }

        public List<DCategorias> Reporte()
        {
            DataTable tablaDatos = Mostrar();

            List<DCategorias> categorias = new List<DCategorias>();

            foreach (DataRow row in tablaDatos.Rows)
            {
                DCategorias categoria = new DCategorias();
                categoria.Categoria = row["Categoria"].ToString();
                categoria.Notas = row["Notas"].ToString();

                categorias.Add(categoria);
            }

            return categorias;
        }
    }
//BUSINESS
//================
using System;
using System.Collections.Generic;
using System.Data;
using CapaDatos;

namespace CapaNegocio
{
    public class NCategorias
    {
        public string Categoria { get; set; }
        public string Notas { get; set; }

    public static DataTable Mostrar()
        {
            return new DCategorias().Mostrar();
        }

        public static List<NCategorias> ReporteCategorias()
        {
            List<DCategorias> categoriasDatos = new DCategorias().Reporte();
            List<NCategorias> categoriasNegocio = new List<NCategorias>();

            foreach (DCategorias categoriaDatos in categoriasDatos)
            {
                NCategorias categoriaNegocio = new NCategorias();
                //categoriaNegocio.IDCategoria = categoriaDatos.IDCategoria;
                categoriaNegocio.Categoria = categoriaDatos.Categoria;
                categoriaNegocio.Notas = categoriaDatos.Notas;

                categoriasNegocio.Add(categoriaNegocio);
            }

            return categoriasNegocio;
        }
    }
//PRESENTATION
//================
//Form with ReportViewer and button BtnPrint

using Microsoft.Reporting.WinForms;
using CapaNegocio;

namespace CapaPresentacion.Formularios
{
    public partial class FormReportes : Form
    {
        public List<NCategorias> reporte = new List<NCategorias>();

        public FormReportes()
        {
            InitializeComponent();
        }

    private void BtnImprimir_Click(object sender, EventArgs e)
        {
            // ReportViewer => AreaReporte
            AreaReporte.LocalReport.DataSources.Clear();

            List<NCategorias> categorias = NCategorias.ReporteCategorias();

            DataTable tablaCategorias = new DataTable("Categorias");
            tablaCategorias.Columns.Add("Categoria", typeof(string));
            tablaCategorias.Columns.Add("Notas", typeof(string));

            foreach (NCategorias categoria in categorias)
            {
                DataRow fila = tablaCategorias.NewRow();
                fila["Categoria"] = categoria.Categoria;
                fila["Notas"] = categoria.Notas;
                tablaCategorias.Rows.Add(fila);
            }

            AreaReporte.LocalReport.DataSources.Clear();
            AreaReporte.LocalReport.DataSources.Add(new ReportDataSource("CategoriasDS", tablaCategorias));
            AreaReporte.LocalReport.ReportEmbeddedResource = "ChronusFutsal.CapaPresentacion.Reportes.RepCategorias.rdlc";

            // Hacemos un refresh al reportViewer
            AreaReporte.RefreshReport();
        }
    }

I have the report named RepCategorias.rdlc with data source Object and Dataset named CategoriaDS from CapaNegocio.NCategorias with the fields Categoria and Notas. I get error in the AreaReporte.RefreshReport() System.NullReferenceException Please could someone tell me the error?

1 Answer 1

0

I found the error!. I just had to change the version of SqlServer.Type and it worked.

Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.