Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a C# windows form containing a details view and a datagrid. Adding a record was working fine, but suddenly stopped working. When working properly, clicking the add button would insert -1, etc. into the ID field which is set to auto number. Now the number fails to appear and the data won't save back to the database. I can input a number and the record will save. I use MySql, and the record insert and auto-mumbering works fine when using Workbench. The form is built with VS 2010 professional, so there is lots of code generated. Where do I look for corrupt code? I have a similar windows form connecting to a different database and it continues to work properly.
Thank you for you help. I really don't want to rebuild the form from scratch.

What I have tried:

I looked for anything obvious with no success. And I rebuilt the form with no luck. Changed from .net 4 to 3.5 with no luck.
Posted
Updated 8-Jul-17 11:57am

The id field should never be exposed to the user. It should also not have a -1 in it unless your code knows what to do with that value. If you're adding records to the table, it shouldn't have a value at all in order to let the database assign the ID value itself.

As for where to look, that depends entirely on your code. We can't see it and know nothing about it so it's pretty much impossible to say.
 
Share this answer
 
Comments
BobbyStrain 6-Jul-17 17:01pm    
I'm not really a programmer, so I use Visual Studio; drag and drop and fill in wizard steps. VS generates tons of code in the designer file. I suppose somewhere in there wires got crossed or code is unstable. As I recall the compiled code was working fine. Now that same file has this strange behavior. Is there a simple way to build an application without all the generated code?
Dave Kreskowiak 6-Jul-17 17:26pm    
Nope. The designer has to generate all that code because it's functionality has to be very generalized to be of any use in any application.

I never use the designers for any database and data display work. They generate too much code that is hidden from you and you have to understand exactly how they work if you run into problems, such as yours. I find they make debugging harder, not easier.

I always write my database and display code by hand. That means it's easy to debug, but it's not easy for someone just starting out to understand.
I found the answer. Quite simple. With the Visual Studio dataset designer one can set properties for each of the columns. The property of interest is auto number. Somehow I inadvertently set it to false. Changed to true, recompiled, and all is well. Restoring my faith in Microsoft stuff. Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900