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

x
?
Solved

Insert a record into two tables from ASP.Net 2.0 from this example

Posted on 2007-11-27
3
Medium Priority
?
242 Views
Last Modified: 2013-11-07
I am using the following example http://www.dotnetbips.com/articles/c1e0ca90-5f5d-47aa-a739-492b562e810a.aspx to create the ability to insert on my webpage using a gridview control. I would like some help figuring how to take some of the fields from the record I am inserting and copy them to another table as well. The insert works great from the example as it is. I just want to be able to copy about 4 of these fields into another table.  I will attach my code so you can see it. as well as the code behind.
here is my main samples.aspx page:_______________________________________________________________________________________
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Samples.aspx.cs" Inherits="Samples" MasterPageFile="~/MasterPage.master" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    &nbsp;
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestADNIConnectionString %>"
        DeleteCommand="DELETE FROM [Samples Received] WHERE [LP] = @LP" InsertCommand="INSERT INTO [Samples Received] ([DNA], [Kit], [Participant], [LP], [Type], [DateRecvd], [DateDrawn], [DateSent], [Time], [Vol], [Sex], [Notes], [VolFrz], [Conc], [Yield], [Ratio], [Available], [CellStatus], [SampleStatus], [Redrawn], [Info], [LabNotes]) VALUES (@DNA, @Kit, @Participant, @LP, @Type, @DateRecvd, @DateDrawn, @DateSent, @Time, @Vol, @Sex, @Notes, @VolFrz, @Conc, @Yield, @Ratio, @Available, @CellStatus, @SampleStatus, @Redrawn, @Info, @LabNotes)"
        SelectCommand="SELECT [DNA], [Kit], [Participant], [LP], [Type], [DateRecvd], [DateDrawn], [DateSent], [Time], [Vol], [Sex], [Notes], [VolFrz], [Conc], [Yield], [Ratio], [Available], [CellStatus], [SampleStatus], [Redrawn], [Info], [LabNotes] FROM [Samples Received]"
        UpdateCommand="UPDATE [Samples Received] SET [DNA] = @DNA, [Kit] = @Kit, [Participant] = @Participant, [Type] = @Type, [DateRecvd] = @DateRecvd, [DateDrawn] = @DateDrawn, [DateSent] = @DateSent, [Time] = @Time, [Vol] = @Vol, [Sex] = @Sex, [Notes] = @Notes, [VolFrz] = @VolFrz, [Conc] = @Conc, [Yield] = @Yield, [Ratio] = @Ratio, [Available] = @Available, [CellStatus] = @CellStatus, [SampleStatus] = @SampleStatus, [Redrawn] = @Redrawn, [Info] = @Info, [LabNotes] = @LabNotes WHERE [LP] = @LP">
        <DeleteParameters>
            <asp:Parameter Name="LP" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="DNA" Type="String" />
            <asp:Parameter Name="Kit" Type="Int32" />
            <asp:Parameter Name="Participant" Type="String" />
            <asp:Parameter Name="Type" Type="String" />
            <asp:Parameter Name="DateRecvd" Type="DateTime" />
            <asp:Parameter Name="DateDrawn" Type="DateTime" />
            <asp:Parameter Name="DateSent" Type="DateTime" />
            <asp:Parameter Name="Time" Type="Int32" />
            <asp:Parameter Name="Vol" Type="Int32" />
            <asp:Parameter Name="Sex" Type="String" />
            <asp:Parameter Name="Notes" Type="String" />
            <asp:Parameter Name="VolFrz" Type="Double" />
            <asp:Parameter Name="Conc" Type="Double" />
            <asp:Parameter Name="Yield" Type="Int32" />
            <asp:Parameter Name="Ratio" Type="Double" />
            <asp:Parameter Name="Available" Type="Int32" />
            <asp:Parameter Name="CellStatus" Type="String" />
            <asp:Parameter Name="SampleStatus" Type="String" />
            <asp:Parameter Name="Redrawn" Type="Boolean" />
            <asp:Parameter Name="Info" Type="String" />
            <asp:Parameter Name="LabNotes" Type="String" />
            <asp:Parameter Name="LP" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="DNA" Type="String" />
            <asp:Parameter Name="Kit" Type="Int32" />
            <asp:Parameter Name="Participant" Type="String" />
            <asp:Parameter Name="LP" Type="Int32" />
            <asp:Parameter Name="Type" Type="String" />
            <asp:Parameter Name="DateRecvd" Type="DateTime" />
            <asp:Parameter Name="DateDrawn" Type="DateTime" />
            <asp:Parameter Name="DateSent" Type="DateTime" />
            <asp:Parameter Name="Time" Type="Int32" />
            <asp:Parameter Name="Vol" Type="Int32" />
            <asp:Parameter Name="Sex" Type="String" />
            <asp:Parameter Name="Notes" Type="String" />
            <asp:Parameter Name="VolFrz" Type="Double" />
            <asp:Parameter Name="Conc" Type="Double" />
            <asp:Parameter Name="Yield" Type="Int32" />
            <asp:Parameter Name="Ratio" Type="Double" />
            <asp:Parameter Name="Available" Type="Int32" />
            <asp:Parameter Name="CellStatus" Type="String" />
            <asp:Parameter Name="SampleStatus" Type="String" />
            <asp:Parameter Name="Redrawn" Type="Boolean" />
            <asp:Parameter Name="Info" Type="String" />
            <asp:Parameter Name="LabNotes" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        DataKeyNames="LP" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" AllowSorting="True" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" >
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="DNA" HeaderText="DNA" SortExpression="DNA" />
            <asp:BoundField DataField="Kit" HeaderText="Kit" SortExpression="Kit" />
            <asp:BoundField DataField="Participant" HeaderText="Participant" SortExpression="Participant" />
            <asp:BoundField DataField="LP" HeaderText="LP" ReadOnly="True" SortExpression="LP" />
            <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
            <asp:BoundField DataField="DateRecvd" HeaderText="DateRecvd" SortExpression="DateRecvd" />
            <asp:BoundField DataField="DateDrawn" HeaderText="DateDrawn" SortExpression="DateDrawn" />
            <asp:BoundField DataField="DateSent" HeaderText="DateSent" SortExpression="DateSent" />
            <asp:BoundField DataField="Time" HeaderText="Time" SortExpression="Time" />
            <asp:BoundField DataField="Vol" HeaderText="Vol" SortExpression="Vol" />
            <asp:BoundField DataField="Sex" HeaderText="Sex" SortExpression="Sex" />
            <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
            <asp:BoundField DataField="VolFrz" HeaderText="VolFrz" SortExpression="VolFrz" />
            <asp:BoundField DataField="Conc" HeaderText="Conc" SortExpression="Conc" />
            <asp:BoundField DataField="Yield" HeaderText="Yield" SortExpression="Yield" />
            <asp:BoundField DataField="Ratio" HeaderText="Ratio" SortExpression="Ratio" />
            <asp:BoundField DataField="Available" HeaderText="Available" SortExpression="Available" />
            <asp:BoundField DataField="CellStatus" HeaderText="CellStatus" SortExpression="CellStatus" />
            <asp:BoundField DataField="SampleStatus" HeaderText="SampleStatus" SortExpression="SampleStatus" />
            <asp:CheckBoxField DataField="Redrawn" HeaderText="Redrawn" SortExpression="Redrawn" />
            <asp:BoundField DataField="Info" HeaderText="Info" SortExpression="Info" />
            <asp:BoundField DataField="LabNotes" HeaderText="LabNotes" SortExpression="LabNotes" />
            <asp:ButtonField CommandName="Insert" Text="Insert" />
        </Columns>
        <EmptyDataTemplate>
            <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
                DataKeyNames="LP" DataSourceID="SqlDataSource1" DefaultMode="Insert" Height="50px"
                Width="125px" OnItemInserted="DetailsView1_ItemInserted">
                <Fields>
                    <asp:BoundField DataField="DNA" HeaderText="DNA" SortExpression="DNA" />
                    <asp:BoundField DataField="Kit" HeaderText="Kit" SortExpression="Kit" />
                    <asp:BoundField DataField="Participant" HeaderText="Participant" SortExpression="Participant" />
                    <asp:BoundField DataField="LP" HeaderText="LP" ReadOnly="True" SortExpression="LP" />
                    <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
                    <asp:BoundField DataField="DateRecvd" HeaderText="DateRecvd" SortExpression="DateRecvd" />
                    <asp:BoundField DataField="DateDrawn" HeaderText="DateDrawn" SortExpression="DateDrawn" />
                    <asp:BoundField DataField="DateSent" HeaderText="DateSent" SortExpression="DateSent" />
                    <asp:BoundField DataField="Time" HeaderText="Time" SortExpression="Time" />
                    <asp:BoundField DataField="Vol" HeaderText="Vol" SortExpression="Vol" />
                    <asp:BoundField DataField="Sex" HeaderText="Sex" SortExpression="Sex" />
                    <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
                    <asp:BoundField DataField="VolFrz" HeaderText="VolFrz" SortExpression="VolFrz" />
                    <asp:BoundField DataField="Conc" HeaderText="Conc" SortExpression="Conc" />
                    <asp:BoundField DataField="Yield" HeaderText="Yield" SortExpression="Yield" />
                    <asp:BoundField DataField="Ratio" HeaderText="Ratio" SortExpression="Ratio" />
                    <asp:BoundField DataField="Available" HeaderText="Available" SortExpression="Available" />
                    <asp:BoundField DataField="CellStatus" HeaderText="CellStatus" SortExpression="CellStatus" />
                    <asp:BoundField DataField="SampleStatus" HeaderText="SampleStatus" SortExpression="SampleStatus" />
                    <asp:CheckBoxField DataField="Redrawn" HeaderText="Redrawn" SortExpression="Redrawn" />
                    <asp:BoundField DataField="Info" HeaderText="Info" SortExpression="Info" />
                    <asp:BoundField DataField="LabNotes" HeaderText="LabNotes" SortExpression="LabNotes" />
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
                </Fields>
            </asp:DetailsView>
        </EmptyDataTemplate>
        <FooterStyle BackColor="#CCCCCC" />
        <RowStyle Font-Size="XX-Small" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <HeaderStyle BackColor="Maroon" Font-Bold="True" Font-Size="X-Small" ForeColor="White" />
        <AlternatingRowStyle BackColor="#CCCCCC" />
    </asp:GridView>
        </asp:Content>
 
samples.aspx.cs------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class Samples : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSourceID = "";
            GridView1.DataBind();
        }
    }
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        GridView1.DataSourceID = "SqlDataSource1";
        GridView1.DataBind();
    }
}

Open in new window

0
Comment
Question by:edi77
[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
3 Comments
 
LVL 1

Accepted Solution

by:
southparksystems earned 2000 total points
ID: 20362502
In the SqlDataSource there is an event called OnInserting. It fires before the update happens. There is also one OnInserted which happens after the intial insert. You can access the parameters of the insert that is about to happen (or already happened) like this:

<asp:SqlDataSource ID="SqlDataSource1" OnInserting="DS_Inserting"
...
...

In the code behind you can access the argument values, command text, etc to do additional inserts/updates as needed:

protected void DS_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        // Access Parameters like this
        e.Command.Parameters[0].Value;

    }
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20591208
Forced accept.

Computer101
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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