Form for inserting new record
Posted on 2008-10-14
I'm building a website in ASP.NET, connecting to a local sql server 2005 database.
I'm working on the page for the "client" table. The page has a formview inside it, with several gridviews that all get statistical information about the client. Next, there is a detailsview that is used to update the client. I set the updatequery, added the fields, added the CommandField for "update / cancel" etc. and it's working.
Now I need to be able to create a new client but I'm having trouble figuring out how to do that. I added the commandfield for "Insert / cancel" and set the insertquery, but some weird things happen and I'm confused.
1) If I don't specify the clientid in my page's GET paremter, my sqldatasource returns zero records and no details view is shown at all, making it impossible to add a new client.
2) If I open an existing client, and then click the "New" link, both the "Insert / Cancel" links and the "Update/Cancel" links show. Clicking Insert does not cause a new client to be created
3) How do I figure out what the new clientID is after inserting? The clientid is an autoincrement primary key field in my sql database.
So... I'm pretty sure I'm missing something somewhere, and this is just my first stab at making simple ASP.NET applications. So maybe I did it wrong. What's the correct way to do it?
Would it be easier to create a brand new page called "client_add" that only inserts, and leave the updating/deleting up to another page? If so how do I do that? Because the page would have to display in "insert" mode automatically without the user having to click on the "New" link...
I don't know. Suggestions are welcome.