Solved

ASP.NET -- insert into multiple tables

Posted on 2011-03-01
4
747 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 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

14 Experts available now in Live!

Get 1:1 Help Now