asp.netlinq-to-entitiesformviewentitydatasource

How to prevent insertion in FormView?


I want to check my form before inserting to prevent insert duplicate ProductSerial in my data base. so how can i check the txtProductSerial.text with my database and if it is duplicate I PREVENT INSERTION. This are my codes

 protected void fvwSoldForm_ItemInserting(object sender, FormViewInsertEventArgs e)
{
    e.Values["DateX"] = DateTime.Now;
    e.Values["IsDeleted"] = false;
    e.Values["Confirmed"] = false;
    var solded = db.SoldedByResellers.ToList();
    solded = solded.Where(p => p.ProductSerial == NumericSerial.Text).ToList();
    if (solded.Count > 0)
        Alert("Please Change the serial code, This code Used before");
//Here WHAT EVER I DO THE INSERTING GOES ON. I WANT TO STOP INSERTING HERE



}

Solution

  • To cancel the operation, set:

    e.Cancel = true;
    

    This will prevent the insert from happening. See the MSDN documentation for an example.