Object reference not set to an instance of an object in procedure return

-2

I have a procedure that returns value, but when it arrives in the ExecuteScalar () of c # it returns me the error "Object reference not set to an instance of an object"

The procedure is correct

            private string IdentificaPat()
            {
                db.Configuration.AutoDetectChangesEnabled = false;
                db.Configuration.ValidateOnSaveEnabled = false;
                string retorno;
                string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {

                    using (SqlCommand cmd = new SqlCommand("Sp_InsertContratoGestores"))
                    {
                        cmd.CommandTimeout = 300;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection = con;
                        con.Open();
                        retorno = cmd.ExecuteScalar().ToString();
                        con.Close();
                    }
                }
                if(retorno == "vazio")
                {
                    return "Vazio";
                }
                else
                {
                    return "Sem novos contratos";
                }

                db.Configuration.AutoDetectChangesEnabled = true;
                db.Configuration.ValidateOnSaveEnabled = true;
            }
    
asked by anonymous 28.03.2018 / 14:30

1 answer

0

I see you are using EF6, so you can simplify this call to.:

db.Database.SqlQuery<string>("Sp_InsertContratoGestores");
    
28.03.2018 / 15:32