Solved

VS2008 Formview Insert Problem

Posted on 2010-11-10
3
461 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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