Solved

ASP.NET -- insert into multiple tables

Posted on 2011-03-01
4
746 Views
Last Modified: 2012-06-21
I have the following code that inserts form values into a single table.   How can I insert into multiple tables?    If, for example, I had a field called address1 that I wanted to insert into the address table from the same form, how do I set that up in the InsertCommand, or can I even do that?
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:KartrisSQLConnection %>" 
                DeleteCommand="DELETE FROM [tblDCPersonTest] WHERE [pers_personid] = @pers_personid" 
                InsertCommand="INSERT INTO [tblDCPersonTest] ([pers_firstname], [pers_middlename], [pers_lastname], [pers_ssn], [pers_dob], [pers_phonehome], [pers_phonework]) VALUES (@pers_firstname, @pers_middlename, @pers_lastname, @pers_ssn, @pers_dob, @pers_phonehome, @pers_phonework)" 
                SelectCommand="SELECT [pers_firstname], [pers_middlename], [pers_lastname], [pers_ssn], [pers_dob], [pers_phonehome], [pers_phonework], [pers_personid] FROM [tblDCPersonTest]" 
                UpdateCommand="UPDATE [tblDCPersonTest] SET [pers_firstname] = @pers_firstname, [pers_middlename] = @pers_middlename, [pers_lastname] = @pers_lastname, [pers_ssn] = @pers_ssn, [pers_dob] = @pers_dob, [pers_phonehome] = @pers_phonehome, [pers_phonework] = @pers_phonework WHERE [pers_personid] = @pers_personid">

                <InsertParameters>
                    <asp:Parameter Name="pers_firstname" Type="String" />
                    <asp:Parameter Name="pers_middlename" Type="String" />
                    <asp:Parameter Name="pers_lastname" Type="String" />
                    <asp:Parameter Name="pers_ssn" Type="String" />
                    <asp:Parameter Name="pers_dob" Type="String" />
                    <asp:Parameter Name="pers_phonehome" Type="String" />
                    <asp:Parameter Name="pers_phonework" Type="String" />
                    <asp:Parameter Name="pers_personid" Type="Object" />
                </InsertParameters>
</sqldatasource>

                        <asp:FormView ID="FormView1" runat="server" DataSourceId="SqlDataSource1" DataKeyNames="pers_personid" DefaultMode="Insert">
                            <InsertItemTemplate> 
                            First Name: <asp:TextBox ID="pers_firstname" runat="server" Text='<%# Bind("pers_firstname") %>'></asp:TextBox><br />
                            Last Name: <asp:TextBox ID="pers_lastname" runat="server" Text='<%# Bind("pers_lastname") %>'></asp:TextBox><br />
                            <div class="submitbuttons">
                            <asp:Button CssClass="button" ID="butSubmit" runat="server" CommandName="Insert" Text="<%$ Resources: Kartris, FormButton_Submit %>" />
                            </div>
                            </InsertItemTemplate>
                        </asp:FormView>

Open in new window

0
Comment
Question by:saturation
4 Comments
 
LVL 13

Accepted Solution

by:
devlab2012 earned 500 total points
ID: 35013869
create a stored procedure and use that stored procedure as InsertCommand. For example:

InsertCommand = "Exec sp_InsertMultipleTb @pers_firstname, @pers_middlename"

This sp will accept the parameters as you are passing to existing Insert Into command. In this sp you can write code to insert records into multiple tables or do whatever you want to. Hope this will give you some idea.
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35013976
Hi, try this example

USE YourDB
GO
INSERT INTO MyTable  (FirstCol, SecondCol)
        VALUES ('First',1);
INSERT INTO MyTable  (FirstCol, SecondCol)
        VALUES ('Second',2);
INSERT INTO MyTable  (FirstCol, SecondCol)
        VALUES ('Third',3);
INSERT INTO MyTable  (FirstCol, SecondCol)
        VALUES ('Fourth',4);
INSERT INTO MyTable  (FirstCol, SecondCol)
        VALUES ('Fifth',5);
GO
0
 

Author Comment

by:saturation
ID: 35014133
Is there a better way to do this?   I understand the stored procedure example, I'm just unfamiliar with "best processes" for .NET.
0
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 35014201
Stored procedures is the best way to insert the records. This lets the db do the db work and the web server do its thing.

If your database is normalized properly, you wouldn't be inserting the same information across multiple tables. The address would be in the address table only and a reference number to it in the person table.

http://databases.about.com/od/specificproducts/a/normalization.htm
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

12 Experts available now in Live!

Get 1:1 Help Now