Stop FV if record exists

Last post 08-08-2008 6:09 PM by JamesSAEP. 2 replies.

Sort Posts:

  • Stop FV if record exists

    08-08-2008, 5:21 PM
    • Member
      66 point Member
    • JamesSAEP
    • Member since 01-15-2008, 11:51 AM
    • Posts 237

    I want to stop a formview from inserting if a record exists in the database (sql 2005).  If you look at my code below, how can I make the FV terminate if the record exists?  I can get the "Label2" to display if the record does exits, but can't stop the FV from inserting.

     

        protected void FormView1Duplicate(object sender, EventArgs e)
    {
    TextBox aTextBox = (TextBox)FormView1.FindControl("VarietalTypeTextBox");
    bool isExist = checkDulEmail(aTextBox.Text);
    if (isExist)
    {
    Label2.Text = "This varietal already exists in the database";
    //How do I stop the FV from Inserting
            }
    else
    {
    //Insert sql here

    }
    }

     

  • Re: Stop FV if record exists

    08-08-2008, 6:01 PM
    Answer
    • Member
      104 point Member
    • subashsagar
    • Member since 08-16-2007, 8:55 PM
    • Posts 27

    Use Eventargs e to cancel the event.

    e.Cancel = true;

    Subash

     

  • Re: Stop FV if record exists

    08-08-2008, 6:09 PM
    • Member
      66 point Member
    • JamesSAEP
    • Member since 01-15-2008, 11:51 AM
    • Posts 237

     Thanks,

    I found this link: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.iteminserting.aspx

    and what I had to do was change the commandarg to

    (Object sender, FormViewInsertEventArgs e)
     and then put in e.Cancel = true inside the if clause.

Page 1 of 1 (3 items)