Solved

Best Method for Saving Data from a ASP.NET Web Form (not MVC) to Multiple Related SQL Server Tables

Posted on 2015-01-11
4
205 Views
Last Modified: 2015-01-11
Hi Experts,

I'm learning, so please bear with me.  This is a non-MVC project so please make the solutions relevant to ASP.NET 4.5.1 Website projects not using Entity Framework.  (I'll get there one of these days but I'm not there yet.)

I've created an unbound web form that contains client data which needs to be saved in multiple related tables, starting with the client record.  Once that is saved I need to use the client ID from the newly saved client record as the foreign key in the client address, telephone, and email records.

Step 1:  Save client name.
Step 2:  Get the Id (primary key) of the new client record.
Step 3:  Save client address (using primary key from step 2 as foreign key in the address record).
Step 4:  Save client phone number (using primary key from step 2 as foreign key in the phone record).
Step 5:  Save client email address (using primary key from step 2 as foreign key in the email record).

I don't need to be told step by step how to do this (I think), but I do need to know the best way to get started with this.

Thanks!
0
Comment
Question by:penlandt
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:sudheeshthegreat
ID: 40543700
Create a stored proc that gets all the data in the web form. Within the stored proc, insert into first table, use SCOPE_IDENTITY() to get the id that was just inserted, and use that when inserting into the other tables. Also, have all the insert statements within a transaction using TRY, COMMIT and ROLLBACK statements.
0
 
LVL 1

Author Comment

by:penlandt
ID: 40543716
OK, I've done that before.  I will need an example of the C# code to execute the stored procedure though.  This is my first time doing this sort of thing so I'm not yet familiar with that.  Thanks!
0
 
LVL 9

Accepted Solution

by:
sudheeshthegreat earned 500 total points
ID: 40543778
If you are using a SQL Server database, you need to use classes in the System.Data.SqlClient namespace.

A sample code can be found here:
http://msdn.microsoft.com/en-us/library/tyy0sz6b(v=vs.110).aspx

Since in your case, you are posting data instead of getting it back, you need to use ExecuteNonQuery instead of ExecuteReader.
0
 
LVL 1

Author Closing Comment

by:penlandt
ID: 40543789
Thank you very much.  I've successfully executed the stored procedure from my app so I'm on my way.  I appreciate the help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now