Return value of the DB in a variable [closed]

1

I'm trying to return the largest ID of the Budget table in a variable, however it returns me an error:

  An unhandled exception of type 'System.NullReferenceException'   occurred in MySql.Data.dll. Additional information:   object not set to an instance of an object. "

Does anyone know what it can be?

Follow the code:

public void ultimoID(int ultimoid)
{
    int resultado;
    var conn = new MySqlConnection(stringCon);
    var sql = "SELECT MAX(id_orcamento)FROM orcamento";
    var cmd = new MySqlCommand(sql, conn);
    Object retorno = cmd.ExecuteScalar();
    resultado = Convert.ToInt32(retorno);
}
    
asked by anonymous 20.09.2017 / 00:48

1 answer

0

Change the Object type to var in your return variable.

var retorno = cmd.ExecuteScalar();

If it is not, make sure your SQL query is generating some result.

    
20.09.2017 / 02:27