Last post Oct 09, 2008 01:43 AM by RickSchultz
Oct 03, 2008 11:07 AM|Jim9674|LINK
Hi All, I originally posted this under the data access and controls area, and was asked to also post it here. I feel this is a pretty common, hot topic that a lot of people have trouble with. Especially when they are just starting out, like me. I'm posting
this because most code examples provided only provide part of the solution, and not the entire solution, or the code example only works for certain things. This may be helpful for experienced programmers, but not so much for newbies like myself. So I'm posting
the entire Button_Click event solution for Adding a New Record to your ASP.NET 2.0 SQL database, using VB.NET code. Hope this helps.
Sample code for Adding a new record to: ASP.NET 2.0, VB.NET, SQL Server 2005 database.
sqlcmd.Connection = sqlcon
ID = sqlcmd.ExecuteScalar()
ID = ID + 1
ID = 1
Oct 03, 2008 12:05 PM|SGWellens|LINK
Note that this code can fail when two inserts are done "simultaneously".
It would be much better to use an auto-increment field in the database.
Oct 03, 2008 12:23 PM|Jim9674|LINK
Understood, and Thank You for your guidance. Though the reasoning for my creating the code this way is because I have a RecordID, which can have duplicate id's, and then a version number of that RecordID(example: RecordID 1, Version 1; RecordID 1, Version
2; RecordID 2, Version 1; etc).
Would it be best, to prevent what you suggest, to have an auto-increment (ID), then my (RecordID), then my (RecordVersion) as my code suggest?
Oct 08, 2008 01:25 AM|RickSchultz|LINK
As a newbie, I greatly appreciate you doing this - I've been looking for something like this for a couple of days now!
As SGWellens suggested, I've used Identity fields for my record IDs (they aren't complex ID's like yours). My issue is, how do I pass those IDs up to my application? For example, I have two tables, a Header & a Detail. When I create a new Header record
in the application (using VB.NET), how do I pass that ID up to the application, and then how do I move the header ID into the Detail ID rows as they are created? I think this is such a basic issue, that no-one is writing about it; a newbie like me has a hard
time finding something this basic.
Thanks anyone for help on this.
Oct 08, 2008 06:18 AM|SGWellens|LINK
My issue is, how do I pass those IDs up to my application?
Oct 09, 2008 01:43 AM|RickSchultz|LINK
Thanks, Steve. I guess I should've noticed the site name first - asp. I'm looking for help with winforms, and while the concept is probably the same, the terminology is different. Sorry for that.
For anyone looking at this post, I did find a great resource at
http://idealprogrammer.com/category/languages/visual-basic-vbnet/; it's 16 tutorials that teach newbies like me how to work with ADO.NET.