Solved

ASP.NET -- insert into multiple tables

Posted on 2011-03-01
4
754 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

622 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