The SelectCommand.Connection property has not been initialized. MySql

0

It turns out that I'm working with SignalR, where in the html view I make the following request to the client:

 var myHub = $.connection.myHub;
        var direccionWeb = $("#direccionWeb").val().toString();
        $.connection.hub.url = direccionWeb + "/signalr";
        $.connection.hub.start().done(function () {
            myHub.server.broadcastMessageToPrinterReportTicket(global_venta, global_mesa, nombregarzon, idgrupo, grupo);

        }).fail(function (error) {
            console.error(error);
        });

In the Hub file I have the following:

public void BroadcastMessageToPrinterReportTicket(String global_venta,String global_mesa,String nombregarzon,String idgrupo, String grupo)
    {
        string nombreImpresora = grupo;
        DataTable dt = new DataTable();

        string sql = "SELECT " +
            "cantidad," +
            "detalle " +
            "FROM producto " +                
            "WHERE venta=@global_venta AND venta>0 AND menu.grupo=@idgrupo AND comandasd.pedido=0";
        conexion.conectar(); 

        using (MySqlCommand command = new MySqlCommand(sql, conexion.con))
        {
            command.Parameters.AddWithValue("@global_venta", global_venta);
            command.Parameters.AddWithValue("@idgrupo", idgrupo);
            using (MySqlDataAdapter reader = new MySqlDataAdapter(command))
            {
                using (MySqlDataAdapter sda = new MySqlDataAdapter(command))
                {
                    sda.Fill(dt);
                }
            }
        }
        conexion.cerrar();


        Clients.All.imprimeReport(datos, ........);
    }

The file that contains the connection class is the following:

public void conectar()
    {
        try
        {
            string ip = HttpContext.Current.Session["ip"].ToString();
            string db = HttpContext.Current.Session["db"].ToString();
            string user = HttpContext.Current.Session["user"].ToString();
            string pass = HttpContext.Current.Session["pass"].ToString();

            con = new MySqlConnection("server=" + ip + ";user id=" + user + ";password=" + pass + ";database=" + db + ";port=3306;Allow User Variables=true"); 
            con.Open();
        }
        catch
        {

        }
    }

But when I try to add data to the DataTable, that is to say on the line where it says sda.Fill (dt), I get the following error: The SelectCommand.Connection property has not been initialized.

The strange thing that when I use is the same function declared in the controller, it works correctly. What I want is to be able to obtain a datatable with the parameters that BroadcastMessageToPrinterReportTicket receives and send it to the client.

If someone knew how to correct the error, or some help, it would be appreciated.

Greetings

    
asked by Danilo 27.12.2017 в 19:51
source

0 answers