Link to home
Start Free TrialLog in
Avatar of SDBen
SDBenFlag for United States of America

asked on

VS2008 Formview Insert Problem

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

ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SDBen

ASKER

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
I don't have exact idea...but in the second case you are dealing directly with the parameters before the command will be executed.