Copy current record fields into an insert record

Last post 05-09-2008 12:25 PM by EddiRae. 6 replies.

Sort Posts:

  • Copy current record fields into an insert record

    05-02-2008, 12:41 PM
    • Loading...
    • EddiRae
    • Joined on 01-20-2008, 9:04 PM
    • Houston
    • Posts 56

    In my last post, I requested knowing how to copy address fields into another address area in the insert template of a formview.  I now need to copy the entire record that is in Readonly mode and open the formview into Insert mode and place all the fields appropriately.

    I understand that I need to place the label fields into variables and also find the textbox fields that are in the insert template, but I am not sure where to place all this activity.

    In the copy of the address, all the code was handled in the PreRenderComplete subroutine.  Is that where this will occur?  Also, since I will be changing modes, I am not sure how that will work either.

     

    Thanks for your help in Advance!!

    Eddi Rae

  • Re: Copy current record fields into an insert record

    05-04-2008, 11:45 PM

    Hi EddiRae ,

    You can store the value in Session scope. And when you come to insert mode , retrieve the values stored in Session .

    see my sample,

     

        protected void FormView1_PreRender(object sender, EventArgs e)
        {
            if (this.FormView1.CurrentMode == FormViewMode.ReadOnly)
            {
                Label name =  this.FormView1.FindControl("countrynameLabel") as Label;
                Session["name"] = name.Text;
            }
            if (this.FormView1.CurrentMode == FormViewMode.Insert)
            {
                TextBox name = FormView1.FindControl("countrynameTextBox") as TextBox;
                name.Text = Session["name"].ToString();
            }
        }
      
        <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
                DeleteCommand="DELETE FROM [country] WHERE [countryid] = @countryid" InsertCommand="INSERT INTO [country] ([countryid], [countryname]) VALUES (@countryid, @countryname)"
                SelectCommand="SELECT * FROM [country]" UpdateCommand="UPDATE [country] SET [countryname] = @countryname WHERE [countryid] = @countryid">
                <DeleteParameters>
                    <asp:Parameter Name="countryid" Type="Int64" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="countryname" Type="String" />
                    <asp:Parameter Name="countryid" Type="Int64" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="countryid" Type="Int64" />
                    <asp:Parameter Name="countryname" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>
            <asp:FormView ID="FormView1" runat="server" DataKeyNames="countryid" DataSourceID="SqlDataSource1"
                Width="133px" OnPreRender="FormView1_PreRender">
               
                <InsertItemTemplate>
                    countryid:
                    <asp:TextBox ID="countryidTextBox" runat="server" Text='<%# Bind("countryid") %>'>
                    </asp:TextBox><br />
                    countryname:
                    <asp:TextBox ID="countrynameTextBox" runat="server" Text='<%# Bind("countryname") %>'>
                    </asp:TextBox><br />
                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                        Text="Insert">
                    </asp:LinkButton>
                    <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="Cancel">
                    </asp:LinkButton>
                </InsertItemTemplate>
                <ItemTemplate>
                    countryid:
                    <asp:Label ID="countryidLabel" runat="server" Text='<%# Eval("countryid") %>'></asp:Label><br />
                    countryname:
                    <asp:Label ID="countrynameLabel" runat="server" Text='<%# Bind("countryname") %>'>
                    </asp:Label><br />
                    <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                        Text="Edit">
                    </asp:LinkButton>
                    <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                        Text="Delete">
                    </asp:LinkButton>
                    <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                        Text="New">
                    </asp:LinkButton>
                </ItemTemplate>
            </asp:FormView>
        
        </div>
        </form>
     
    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Copy current record fields into an insert record

    05-05-2008, 3:12 AM
    Answer

    you can use FormView_Databound event like this:

    protected void FormView1_DataBound(object sender, EventArgs e)

    {

    if(FormView1.CurrentMode == FormViewMode.Readonly)

    {

    Label custno = (Label )FormView.FindControl("cust_noLabel ");

    Label shipid = (Label )FormView.FindControl("shipidLabel ");

    Session["custno"]=custno.Text;

    Session["shipid"]=shipid.Text;

    }

    if (FormView1.CurrentMode == FormViewMode.Insert)

    {

    TextBox custno = (TextBox)FormView2.FindControl(
    "cust_noTextBox");

    TextBox shipid = (TextBox)FormView2.FindControl("ship_idTextBox");

    custno.Text=Convert.ToString(Session["custno"]);

    shipid.Text=Convert.ToString(Session["shipid"]);

    }

    }

  • Re: Copy current record fields into an insert record

    05-08-2008, 1:43 PM
    • Loading...
    • EddiRae
    • Joined on 01-20-2008, 9:04 PM
    • Houston
    • Posts 56

    I am trying to do this in the procedure on the form.  Here is what I have. 

     

    Protected Sub cmdCopyRec_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCopyRec.Click
              Dim lblStatus As Label
              Dim ddlStatus As DropDownList
    
              lblStatus = fvMailout.FindControl("lblStatus")
              Session("Status") = lblStatus.Text
    
              fvMailout.DefaultMode = FormViewMode.Insert
    
              ddlStatus = fvMailout.FindControl("ddlStatus")
              ddlStatus.Text = Session("Status").ToString
    End Sub

     I am getting this error:

    Object reference not set to an instance of an object. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error: 
    
    
    Line 301:          fvMailout.DefaultMode = FormViewMode.Insert
    Line 302:          ddlStatus = fvMailout.FindControl("ddlStatus")
    Line 303:          ddlStatus.Text = Session("Status").ToString
     
    
    Source File: D:\Consulting\AMS\Applications\fpccarecentre\fpcmailouts_CANADA_TEST\mailouts\ViewRecord.aspx.vb    Line: 303 
    
    Stack Trace: 
    
    
    [NullReferenceException: Object reference not set to an instance of an object.]
       mailouts_ViewRecord.cmdCopyRec_Click(Object sender, EventArgs e) in D:\Consulting\AMS\Applications\fpccarecentre\fpcmailouts_CANADA_TEST\mailouts\ViewRecord.aspx.vb:303
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
    
     
    
    
    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.50727.312; ASP.NET Version:2.0.50727.833 
  • Re: Copy current record fields into an insert record

    05-09-2008, 9:03 AM
    Answer

    you cannot find insertitem template control on click of a button. you have to use formview_databound event to capture insertitem template control.

  • Re: Copy current record fields into an insert record

    05-09-2008, 10:28 AM
    • Loading...
    • EddiRae
    • Joined on 01-20-2008, 9:04 PM
    • Houston
    • Posts 56

    That worked!!  In the Command Button I changed the mode of the formview.  In the formview "ModeChanged" procedure is where I am doing the copy. 

    Thanks for the info!!!

  • Re: Copy current record fields into an insert record

    05-09-2008, 12:25 PM
    • Loading...
    • EddiRae
    • Joined on 01-20-2008, 9:04 PM
    • Houston
    • Posts 56

    I found that it was not working in ModeChanged.  There are 3 things that I did to get this to work.

    1.  When I click the command button to do the copy, I had a Session field that said I clicked this button. At this time I changed the formview to be insert mode

         Protected Sub cmdCopyRec_Click(ByVal sender As Object, ByVal e As System.EventArgs)
              Session("fvMailoutSub") = "CopyRec"
              fvMailout.ChangeMode(FormViewMode.Insert)
         End Sub

    2.   In the Databound, that is where I copied the fields to Session fields:

         Protected Sub fvMailout_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvMailout.DataBound
              Dim lblStatus As Label
              Dim lblName As Label

              If fvMailout.CurrentMode = FormViewMode.ReadOnly Then
                   lblStatus = fvMailout.FindControl("lblStatus")
                   Session("Status") = lblStatus.Text

                   lblName = fvMailout.FindControl("lblName")

                   Session("Name") = lblName.Text
              End If
         End Sub

     3. In the Page PreRenderComplete, I copied the Session fields to the TEXT fields

    Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
              Dim ddlStatus As DropDownList
              Dim NameTextBox As TextBox
              
              If fvMailout.CurrentMode = FormViewMode.Insert Then
                   If Session("fvMailoutSub") = "CopyRec" Then
                        Session("fvMailoutSub") = Nothing
    
                        ddlStatus = fvMailout.FindControl("ddlStatus")
                        ddlStatus.Text = Session("Status").ToString
    
                        NameTextBox = fvMailout.FindControl("NameTextBox")
                        NameTextBox.Text = Session("Name")
                   End If
              End If
         End Sub

     Again, thanks for all your help!!!

    I think I am now understanding all this .net coding

Page 1 of 1 (7 items)