I'm trying to make a method that returns the last record of the table but gives error:
"Coditem" operation failed
public int UltimoItem()
{
con = conexao.obterConexao();
try
{
cmd = new SqlCommand("SELECT MAX(Cod_Item) FROM Pedidos_Itens", con);
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
int Ultimo = 0;
while (dr.Read())
{
Ultimo = Convert.ToInt32(dr["Cod_Item"]);
}
return Ultimo;
}
catch (Exception ex)
{
throw new Exception("Falha na operação: " + ex.Message);
}
finally
{
con.Close();
}
}