ERROR: cannot Convert from 'int' to 'CRUD.Models.Times'

Asked

Viewed 39 times

0

hello! I am doing a crud project on Asp net mvc and my code presents the following error:

Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot Convert from 'int' to 'CRUD.Models.Times'

This error occurs when I pass the id parameter into the Try.

public ActionResult ExcluirTime(int id)
        {
            try
            {
                _repositorio = new TimeRepositorio();
                if(_repositorio.ExcluirTime(id)) //o erro se dá no id
                {
                    ViewBag.Message("Time excluído com sucesso!");
                }
                return RedirectToAction("MostrarTimes");
            }
            catch (Exception)
            {
                return View("MostrarTimes");
            }
        }

Class Excluirtime

public bool ExcluirTime(Times objTimes)
        {
            int i;
            Connection();
            using(SqlCommand consulta = new SqlCommand("ExcluirTime", _con))
            {
                consulta.CommandType = CommandType.StoredProcedure;
                _con.Open();
                consulta.Parameters.AddWithValue("@time", objTimes.cd_time);
                i = consulta.ExecuteNonQuery();
            };
            _con.Close();
            if(i >= 1)
            {
                return true;
            }
            return false;
        }
  • Also put the function ExcluirTime class TimeRepositorio in your question so we can help you

  • ready, put on

  • The function expects an object of the type Times and not an id, you need to search among all teams the one that has the desired id and only then pass this team as a function parameter

  • 1

    now it’s gone! thank you

1 answer

1


The method in your repository does not expect an argument int which in your case is Id. You can change the method to receive the correct parameter and thus avoid performing another query only or instantiating a new object only to perform this operation. And I would recommend changing a bit so you don’t have to manually close the connection, but for that you’d need to see how you’re managing it.

public bool ExcluirTime(int cd_time)
{
    int i;

    Connection();
    using(SqlCommand consulta = new SqlCommand("ExcluirTime", _con))
    {
        consulta.CommandType = CommandType.StoredProcedure;
        _con.Open();
        consulta.Parameters.AddWithValue("@time", cd_time);
        i = consulta.ExecuteNonQuery();
    };
    _con.Close();
    if(i >= 1)
    {
        return true;
    }
    return false;
}
  • thank you so much! that’s right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.