Click event saving duplicate records into the database

  • Thread starter Thread starter Hezal
  • Start date Start date
H

Hezal

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.AddCustomer.Click += new System.EventHandler(this.AddCustomer_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.
 
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_Click method, does it get
called twice?
 
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.AddCustomer.Click += new
System.EventHandler(this.AddCustomer_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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top