Solved

VS2008 Formview Insert Problem

Posted on 2010-11-10
3
466 Views
Last Modified: 2012-08-13
Very odd issue with Formview in VS2008.

Envrionment: Sql 2005, VS2008, VB.NET
Setting: Formview bound to a sqldatasource. Has an embedded fileupload control. Clicking on insert fires the upload of the selected file to the database - works fine. However, I need to pass several other fields from the datasource to the table with the file. (definitions in code windows).  

If you look at the code_behind coding, the two fields associated with the fileupload control (attachname and attachfile) get posted to the table. The remaining fields don't get passed, regardless of whether they are set to variables, defaults or constant. Not sure what is going on.
 
Formview insert template:

                                        <InsertItemTemplate>
                                            File Name:
                                            <asp:TextBox ID="AttachNameTextBox" runat="server" Text='<%# Bind("AttachName") %>' />
                                            &nbsp; File: &nbsp;
                                            <asp:TextBox ID="WorkOrderIDTextBox" runat="server" Text='<%# Bind("WorkOrderID") %>'
                                                Visible="false" />
                                            <asp:TextBox ID="DocLinkNoTextBox" runat="server" Text='<%# Bind("DocLinkNo") %>'
                                                Visible="False" />
                                            <asp:TextBox ID="AttachDateTextBox" runat="server" Text='<%# Bind("AttachDate") %>'
                                                Visible="False" />
                                            <asp:TextBox ID="AttachByTextBox" runat="server" Text='<%# Bind("AttachBy") %>' Visible="False" />
                                            <asp:FileUpload ID="FileUpload2" runat="server" FileBytes='<%# Bind("AttachFile") %>' />
                                            <br />
                                            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                                                Text="Upload File" />
                                            &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
                                                CommandName="Cancel" Text="Cancel Upload" />
                                        </InsertItemTemplate>

Open in new window


 
bound datasource:
                                       <asp:SqlDataSource ID="DocAttachDS" runat="server" ConflictDetection="CompareAllValues"
                                            ConnectionString="<%$ ConnectionStrings:ARDDBConnectionString %>" DeleteCommand="DELETE FROM [tblWorkOrderAttachments] WHERE [AttachRecID] = @original_AttachRecID AND (([WorkOrderID] = @original_WorkOrderID) OR ([WorkOrderID] IS NULL AND @original_WorkOrderID IS NULL)) AND (([DocLinkNo] = @original_DocLinkNo) OR ([DocLinkNo] IS NULL AND @original_DocLinkNo IS NULL)) AND (([Attached] = @original_Attached) OR ([Attached] IS NULL AND @original_Attached IS NULL)) AND (([AttachDate] = @original_AttachDate) OR ([AttachDate] IS NULL AND @original_AttachDate IS NULL)) AND (([AttachBy] = @original_AttachBy) OR ([AttachBy] IS NULL AND @original_AttachBy IS NULL)) AND (([AttachName] = @original_AttachName) OR ([AttachName] IS NULL AND @original_AttachName IS NULL)) AND (([AttachFile] = @original_AttachFile) OR ([AttachFile] IS NULL AND @original_AttachFile IS NULL))"
                                            InsertCommand="INSERT INTO [tblWorkOrderAttachments] ([WorkOrderID], [DocLinkNo], [Attached], [AttachDate], [AttachBy], [AttachName], [AttachFile]) VALUES (@WorkOrderID, @DocLinkNo, @Attached, @AttachDate, @AttachBy, @AttachName, @AttachFile)"
                                            OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [WorkOrderID], [DocLinkNo], [Attached], [AttachDate], [AttachBy], [AttachName], [AttachFile], [AttachRecID] FROM [tblWorkOrderAttachments]"
                                            UpdateCommand="UPDATE [tblWorkOrderAttachments] SET [WorkOrderID] = @WorkOrderID, [DocLinkNo] = @DocLinkNo, [Attached] = @Attached, [AttachDate] = @AttachDate, [AttachBy] = @AttachBy, [AttachName] = @AttachName, [AttachFile] = @AttachFile WHERE [AttachRecID] = @original_AttachRecID AND (([WorkOrderID] = @original_WorkOrderID) OR ([WorkOrderID] IS NULL AND @original_WorkOrderID IS NULL)) AND (([DocLinkNo] = @original_DocLinkNo) OR ([DocLinkNo] IS NULL AND @original_DocLinkNo IS NULL)) AND (([Attached] = @original_Attached) OR ([Attached] IS NULL AND @original_Attached IS NULL)) AND (([AttachDate] = @original_AttachDate) OR ([AttachDate] IS NULL AND @original_AttachDate IS NULL)) AND (([AttachBy] = @original_AttachBy) OR ([AttachBy] IS NULL AND @original_AttachBy IS NULL)) AND (([AttachName] = @original_AttachName) OR ([AttachName] IS NULL AND @original_AttachName IS NULL)) AND (([AttachFile] = @original_AttachFile) OR ([AttachFile] IS NULL AND @original_AttachFile IS NULL))">
                                            <DeleteParameters>
                                                <asp:Parameter Name="original_AttachRecID" Type="Int32" />
                                                <asp:Parameter Name="original_WorkOrderID" Type="Int32" />
                                                <asp:Parameter Name="original_DocLinkNo" Type="Int32" />
                                                <asp:Parameter Name="original_Attached" Type="Boolean" />
                                                <asp:Parameter DbType="DateTime" Name="original_AttachDate" />
                                                <asp:Parameter Name="original_AttachBy" Type="String" />
                                                <asp:Parameter Name="original_AttachName" Type="String" />
                                                <asp:Parameter Name="original_AttachFile" />
                                            </DeleteParameters>
                                            <UpdateParameters>
                                                <asp:Parameter Name="WorkOrderID" Type="Int32" />
                                                <asp:Parameter Name="DocLinkNo" Type="Int32" />
                                                <asp:Parameter Name="Attached" Type="Boolean" />
                                                <asp:Parameter DbType="DateTime" Name="AttachDate" />
                                                <asp:Parameter Name="AttachBy" Type="String" />
                                                <asp:Parameter Name="AttachName" Type="String" />
                                                <asp:Parameter Name="AttachFile" Type="Object" />
                                                <asp:Parameter Name="original_AttachRecID" Type="Int32" />
                                                <asp:Parameter Name="original_WorkOrderID" Type="Int32" />
                                                <asp:Parameter Name="original_DocLinkNo" Type="Int32" />
                                                <asp:Parameter Name="original_Attached" Type="Boolean" />
                                                <asp:Parameter DbType="DateTime" Name="original_AttachDate" />
                                                <asp:Parameter Name="original_AttachBy" Type="String" />
                                                <asp:Parameter Name="original_AttachName" Type="String" />
                                                <asp:Parameter Name="original_AttachFile" />
                                            </UpdateParameters>
                                            <InsertParameters>
                                                <asp:Parameter Name="WorkOrderID" Type="Int32" />
                                                <asp:Parameter Name="DocLinkNo" Type="Int32" />
                                                <asp:Parameter Name="Attached" Type="Boolean" />
                                                <asp:Parameter Name="AttachDate" Type="DateTime" />
                                                <asp:Parameter Name="AttachBy" Type="String" />
                                                <asp:Parameter Name="AttachName" Type="String" />
                                                <asp:Parameter Name="AttachFile" />
                                            </InsertParameters>
                                        </asp:SqlDataSource>

Open in new window

code behind for ItemInserting:

   Protected Sub DocUploadView_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
        Dim DocUploadDS As SqlDataSource = TryCast(WorkRequest.FindControl("DocAttachDS"), SqlDataSource)
        Dim FileUpload2 As FileUpload = TryCast(WorkRequest.FindControl("FileUpload2"), FileUpload)
        If FileUpload2.HasFile Then
            e.Values("AttachName") = FileUpload2.FileName
            e.Values("AttachFile") = FileUpload2.FileBytes
            e.Values("AttachDate") = DateTime.Now
            e.Values("Attached") = True
            e.Values("WorkOrderID") = Session("WRNum").ToString
        Else
            e.Cancel = True
        End If
    End Sub

Open in new window

0
Comment
Question by:SDBen
[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
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 34107110
may be you might want to try OnInserting of SqlDataSource to set parameter value like
e.Command.Parameters("@AttachDate").Value = DateTime.Now
0
 

Author Closing Comment

by:SDBen
ID: 34107659
Thanks!!! That worked very nicely. Curious as to why the first version didn't work - would have though the parameters were sent either way.

Much appreciated!

Ben
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 34114215
I don't have exact idea...but in the second case you are dealing directly with the parameters before the command will be executed.
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

724 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