Conversion failed when Converting the varchar value 'COL' to data type int

Asked

Viewed 173 times

1

I’m trying to register a project in a certain area, but every time I try to register it returns an exception.

Exception:

An Exception of type 'System.Data.Entity.Infrastructure.Dbupdateexception' occurred in Entityframework.dll but was not handled in user code

Conversion failed when Converting the varchar value 'COL' to data type int.

My controller:

// GET: Projeto/Create
public ActionResult Create()
    {
      ViewBag.proArea = new SelectList(db.tblArea, "areIdArea", "areArea");
      return View();
    }

// POST: Projeto/Create
   [HttpPost]
   [ValidateAntiForgeryToken]
   public ActionResult Create([Bind(Include = "proIdProjeto,proNome,proArea")] tblProjeto tblProjeto)
   {
     if (ModelState.IsValid)
      {
         db.tblProjeto.Add(tblProjeto);
         db.SaveChanges();
         return RedirectToAction("Index");
      }

      ViewBag.proArea = new SelectList(db.tblArea, "areIdArea", "areArea", tblProjeto.proArea);
        return View(tblProjeto);
     }

My Class:

public partial class tblProjeto
    {

        [Key]
        public int proIdProjeto { get; set; }
        [Required(ErrorMessage = "Inform o nome deo projeto")]
        [Display(Name = "Nome")]
        public string proNome { get; set; }
        [Required(ErrorMessage = "informe a area")]
        [Display(Name = "Area")]
        public Nullable<int> proArea { get; set; }

        public virtual tblArea tblArea { get; set; }
     }

I don’t really know how to fix it, I researched it here, but I didn’t find any examples that came close to my need. I’d like to know why you keep making this exception and how can I fix it? I ask for your help and I’m sorry if anything is wrong.

1 answer

2


In your View, the field where you are being filled with 'COL' in Visual Studio may be as string, but when sending the command to insert in the bank, there is a conflict of types, probably in the bank as int... Or if you’re sending like FK, you must be passing the value instead of ID.

Check the data typing if you need to send to View in another way, create a ViewModel so you can do, roughly, however you want...

Browser other questions tagged

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