Error "ExecuteNonQuery: Connection property has not been initialized."

1

Why do you return the following error?

  

ExecuteNonQuery: Connection property has not been initialized.

Code:

public bool IngresarUsuario(Prueba objeto)
{
    using (SqlConnection con = new SqlConnection(strConnection))
    {
        try
        {                
            query= ("insert into Pruebas (Id,Nombre,Componente)values (@Id,@Nombre,@Componente)");
            SqlCommand comando = new SqlCommand(query,con);
            comando.CommandType = CommandType.Text;
            con.Open();
            comando.Connection = cadena;
            comando.Parameters.AddWithValue("@Id", objeto.Id);
            comando.Parameters.AddWithValue("@Nombre", objeto.Nombre);
            comando.Parameters.AddWithValue("@Componente", objeto.Componente);
            if (Convert.ToBoolean(comando.ExecuteNonQuery()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        finally { }
    }
}
    
asked by Jesus Galeano 07.11.2016 в 22:38
source

3 answers

0

open the connection before assigning it to the SQLCommand.

public bool IngresarUsuario(Prueba objeto)
{
    using (SqlConnection con = new SqlConnection(strConnection))
    {
        try
        {  
            con.Open();     

            query= ("insert into Pruebas (Id,Nombre,Componente)values (@Id,@Nombre,@Componente)");
            SqlCommand comando = new SqlCommand(query,con);
            comando.CommandType = CommandType.Text;

            comando.Connection = cadena;
            comando.Parameters.AddWithValue("@Id", objeto.Id);
            comando.Parameters.AddWithValue("@Nombre", objeto.Nombre);
            comando.Parameters.AddWithValue("@Componente", objeto.Componente);
            if (Convert.ToBoolean(comando.ExecuteNonQuery()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch(SqlException sqlEx) 
        {
        // aquí hacer log o lo que sea
        }
        catch(Exception ex)
        {
        // aquí hacer log o lo que sea
        }
        finally 
        {
        // ¿Es necesario este bloque?
        }
    }
}
    
answered by 10.11.2016 / 15:28
source
1

Try removing the following line:

comando.Connection = cadena; // cadena == null ??

I do not know what cadena is (you do not show it), but it does not seem right that you're using it here and it's probably null . In any case, you do not need to do that, since the connection is passing it to the object comando in its constructor:

SqlCommand comando = new SqlCommand(query,con); // ya le estás pasando "con" aquí
    
answered by 07.11.2016 в 22:57
1

It is not necessary to call Connection again because when you do this ...

     SqlCommand comando = new SqlCommand(query,con);

You automatically indicate the query and the connection you are going to make. You also need to close the connection if not, when you want to open another, you will mark error.

    
answered by 07.11.2016 в 23:11