The Instance of my class does not take me Connection

0

This is my Conexion class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using System.Data.SqlClient;
namespace CapaAccesoDatos
public class Conexion
{

    /*Patron Singleton*/

    private static readonly Conexion _instancia = new Conexion();
    public static Conexion Instancia
    {
        get { return Conexion.Instancia; }
    }

    /*Metodo de conexion*/

    public SqlConnection Conectar()
    {
        try
        {
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = "Data source=.;Initital Catalog=Sys_CarritoCompra;Integrated Security=true";
            return cn;
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }



}

}

And now I want to call that instance to my class Category:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using System.Data.SqlClient;
using CapaEntidades;

    public List<entCategoria> ListarCategoria()
    {
        SqlCommand cmd = null;
        SqlDataReader dr = null;
        List<entCategoria> lista = null;
        try
        {
            SqlConnection cn = new Conexion.Instancia.Conectar();
            cmd = new SqlCommand("sp_ListarCategorias", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cn.Open();
            dr = cmd.ExecuteReader();
            lista = new List<entCategoria>();
            while (dr.Read())
            {
                entCategoria objCat = new entCategoria();
                objCat.CatId =Convert.ToInt32(dr["CatId"]);
                objCat.CatCodigo = Convert.ToString(dr["CatCodigo"]);
                objCat.CatNombre = Convert.ToString(dr["CatNombre"]);

                lista.Add(objCat);
            }


        }
        catch(Exception ex)
        {
            throw ex;
        }
        finally
        {
            cmd.Connection.Close();
        }
        return lista;

    }
    
asked by Kevin Quevedo 13.05.2018 в 09:53
source

1 answer

0

Notice that you are never creating the connection object. I have added a constructor to the connection class and I have modified the Instance property so that it creates the object if it does not exist.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using System.Data.SqlClient;
namespace CapaAccesoDatos
public class Conexion
{

/*Patron Singleton*/

private static readonly Conexion _instancia = new Conexion();
public static Conexion Instancia
{
    get {
        if (_instancia == null)
            _instancia = new Conexion();

        return _instancia;
    }
}

// Constructor
protected Conexion()
{
}

/*Metodo de conexion*/
public SqlConnection Conectar()
{
    try
    {
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = "Data source=.;Initital Catalog=Sys_CarritoCompra;Integrated Security=true";
        return cn;
    }
    catch(Exception ex)
    {
        throw ex;
    }
}
}
    
answered by 13.05.2018 в 10:04