Solved

ASP.NET -- insert into multiple tables

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

772 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