Click event saving duplicate records into the database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SGV6YWw=?=

    Click event saving duplicate records into the database

    Hi,
    I am trying to add a new record to a table but everytime I click the button,
    somehow it saves the record twice...
    I've created a stored procedure to insert records into a table and I called
    that procedure in the code that I attached to a button click.
    Here is the event handler code which is created automatically:
    this.AddCustome r.Click += new System.EventHan dler(this.AddCu stomer_Click);

    I checked the stored procedure and the code attached to the button and I
    can't see what is wrong.
    I would appreciate any suggestions for any possible solution. Thanks.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Click event saving duplicate records into the database

    Is it possible you are adding the event handler twice? That the code to
    add to the Click event has been added twice?

    If you place a breakpoint in the AddCustomer_Cli ck method, does it get
    called twice?


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Hezal" <Hezal@discussi ons.microsoft.c omwrote in message
    news:71CC9116-BD63-4DA3-B035-F020BF4A35DB@mi crosoft.com...
    Hi,
    I am trying to add a new record to a table but everytime I click the
    button,
    somehow it saves the record twice...
    I've created a stored procedure to insert records into a table and I
    called
    that procedure in the code that I attached to a button click.
    Here is the event handler code which is created automatically:
    this.AddCustome r.Click += new System.EventHan dler(this.AddCu stomer_Click);
    >
    I checked the stored procedure and the code attached to the button and I
    can't see what is wrong.
    I would appreciate any suggestions for any possible solution. Thanks.

    Comment

    • Peter Duniho

      #3
      Re: Click event saving duplicate records into the database

      On Mon, 11 Feb 2008 12:38:15 -0800, Hezal
      <Hezal@discussi ons.microsoft.c omwrote:
      Hi,
      I am trying to add a new record to a table but everytime I click the
      button,
      somehow it saves the record twice...
      I've created a stored procedure to insert records into a table and I
      called
      that procedure in the code that I attached to a button click.
      Here is the event handler code which is created automatically:
      this.AddCustome r.Click += new
      System.EventHan dler(this.AddCu stomer_Click);
      >
      I checked the stored procedure and the code attached to the button andI
      can't see what is wrong.
      I would appreciate any suggestions for any possible solution. Thanks.
      You didn't post anything that could be used to identify the problem. You
      say you checked things, but if you'd checked everything you needed to and
      not missed anything, you'd have found the problem and wouldn't be posting
      here. So either there's something you didn't check, or there's something
      you missed in something you did check. Either way, without posting the
      things that need checking, there's no way for anyone else to tell you
      what's wrong.

      That said: if the records is getting saved twice, obviously some code that
      saves the record is getting executed twice. The first thing you need to
      do is figure out what's getting executed twice. Set a breakpoint at the
      part of your code closest to the database and see how many times you hit
      it for a given button click.

      If it gets hit just once, then the problem is somewhere in your database
      side. If it gets hit twice, then there's something wrong with the code
      side. Even better, you can look at the call stack for each time it gets
      hit and get an idea for why it's getting called each time. One time will
      look like what you expect. The other won't. The one that isn't what you
      expect is where you have a problem.

      Your post implies that you're not writing the code to add the Click event
      handler yourself. If it weren't for that, the thing I'd be most likely to
      suspect is that you're adding the event handler twice. If I've
      misinterpreted the implication, then IMHO that's the first thing you
      should look for: how many times are you subscribing the event handler? It
      should only be once, but if it's twice you'll find it getting executed
      twice for each Click event.

      Pete

      Comment

      Working...