Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Click event saving duplicate records into the database

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hezal - 11 Feb 2008 20:38 GMT
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.
Nicholas Paldino [.NET/C# MVP] - 11 Feb 2008 21:21 GMT
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?

Signature

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

> Hi,
> I am trying to add a new record to a table but everytime I click the
[quoted text clipped - 9 lines]
> can't see what is wrong.
> I would appreciate any suggestions for any possible solution. Thanks.
Peter Duniho - 11 Feb 2008 21:35 GMT
> Hi,
> I am trying to add a new record to a table but everytime I click the  
[quoted text clipped - 10 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.