Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys wondering if guys could tell me if i'm on the right path, im trying to check if data(iccid) exists in database. Check code below.

C#
public ActionResult Add(SIMCardInfoView model)
        {
            if (ModelState.IsValid)
            {
                SIMCard simCard = null;
                //Check if entity exists in database 
                simCard = db.SIMCards.FirstOrDefault(card => card.ICCID == model.ICCID);

                if (model.IsNew)
                {
                    // The posted model indicated this is a new simcard.
                    simCard = new SIMCard();
                    db.SIMCards.Add(simCard);
                }
                else
                {
                    // This model already exists
                    // The entity could not be saved, add an error and return to the view.
                    ModelState.AddModelError("Validation", "Unable to save this simcard. The database entity exists in the database.");
                    UpdateDetailViewModel(model);
                    return View(model);
                }

                //TODO:Default group
                //simCard.GroupID = 1;
                simCard.CapturedBy = "Test";
                simCard.DateCaptured = DateTime.Now;
                simCard.ICCID = model.ICCID;
                simCard.IMSI = model.IMSI;
                simCard.MSISDN = model.MSISDN;
                simCard.NetworkID = model.NetworkID;
                simCard.PackageID = model.PackageID;
                simCard.SIMCardID = model.SIMCardID;
                simCard.SubBatchID = model.SubBatchID;
                simCard.TrackingCode = model.TrackingCode;

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                // The user did not post a valid model.
                // To debug this, check the errors inside the modelstate object.
                UpdateDetailViewModel(model);
                return View(model);
            }
        }


Can someone maybe point out where i went wrong i know im being a noob here.
Posted
Updated 15-Oct-15 21:00pm
v2

1 solution

You can use Any() method here like:-

C#
bool hasData = db.SIMCards.Any(card => card.ICCID == model.ICCID);
if (hasData ){
  // entity exists in database
}
else {
  // nope, not exists in database
}
 
Share this answer
 
Comments
Member 11838038 16-Oct-15 3:15am    
Question

protected void UpdateDetailViewModel(SIMCardInfoView model)
{
model.Networks = new SelectList(db.Networks, "NetworkID", "Name");
model.Packages = new SelectList(db.Packages, "PackageID", "Name");
//model.Groups = new SelectList(db.Groups, "GroupID", "Name");
model.SubBatches = new SelectList(db.SubBatches, "SubBatchID", "Code");
model.Batches = new SelectList(db.Batches, "BatchID", "Name");
model.IsNew = model.SIMCardID == 0;
}

is there anyway i could use isNew instead of hasData
Palash Mondal_ 16-Oct-15 3:33am    
model.IsNew is used for different purpose, to find if it is a insert or update and hasData is used for different purpose, to check entity exists in database. You can not combine them both here.
Member 11838038 16-Oct-15 3:37am    
Okay thanks man
Member 11838038 16-Oct-15 3:17am    
where model.IsNew = model.SIMCardID == 0;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900