Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET -- insert into multiple tables

Posted on 2011-03-01
4
Medium Priority
?
756 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

705 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