Formview FileUpload and ObjectDataSource

Discussion in 'ASP .Net' started by Chris, Mar 31, 2007.

  1. Chris

    Chris Guest

    I want do my insert to a database using a formview bound to an
    objectdatasource. I also want to upload a file. I'm quite new to and this is
    just test code but Is there any way I can pass the file as a byte array and
    manipulate it in my business object. Currently what I have is this, but my
    byte array in my business object is nothing. It's like it's not passed the
    file as a stream. I think I am not binding the FileUpload to the
    insertparameter but I don't know how to fix it. Regards, Chris.


    <asp:FormView ID="frmvwCustomer" runat="server" DataSourceID="objdsCustomer"
    DefaultMode="Insert" AllowPaging="True">

    <InsertItemTemplate>

    Name<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />

    Email<asp:TextBox ID="txtemail" Text="<%# Bind('strEmail') %>"
    runat="server"></asp:TextBox><br />

    CV<asp:FileUpload ID="CV" runat="server" /><br />

    &nbsp;<asp:Button ID="Button1" runat="server" CommandName="Insert"
    Text="Button" />

    </InsertItemTemplate>

    </asp:FormView>


    </div>

    <asp:ObjectDataSource ID="objdsCustomer" runat="server"
    InsertMethod="createcustomer"

    SelectMethod="getcustomer_list" TypeName="Customer">

    <InsertParameters>

    <asp:parameter Name="strName" Type="Object" />

    <asp:parameter Name="strEmail" Type="Object" />

    <asp:parameter Name="CV" Type="Object" />

    </InsertParameters>

    </asp:ObjectDataSource>





    Public Sub createcustomer(ByVal strname As String, ByVal strEmail As String,
    ByVal cv() As Byte)

    End Sub
     
    Chris, Mar 31, 2007
    #1
    1. Advertising

  2. some sample code here may help

    http://www.superexpert.com/Books/AspNet2Unleashed/Chapters/Chapter11/Listing11_26.aspx
    http://vinayakshrestha.wordpress.com/2007/03/31/ajax-extensions-file-upload-workaround/

    general formview info is here:

    http://msconline.maconstate.edu/tutorials/ASPNET20/default.htm




    "Chris" <> wrote in message news:...
    >I want do my insert to a database using a formview bound to an objectdatasource. I also want to upload a file. I'm quite new to and
    >this is just test code but Is there any way I can pass the file as a byte array and manipulate it in my business object. Currently
    >what I have is this, but my byte array in my business object is nothing. It's like it's not passed the file as a stream. I think I
    >am not binding the FileUpload to the insertparameter but I don't know how to fix it. Regards, Chris.
    >
    >
    > <asp:FormView ID="frmvwCustomer" runat="server" DataSourceID="objdsCustomer" DefaultMode="Insert" AllowPaging="True">
    >
    > <InsertItemTemplate>
    >
    > Name<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
    >
    > Email<asp:TextBox ID="txtemail" Text="<%# Bind('strEmail') %>" runat="server"></asp:TextBox><br />
    >
    > CV<asp:FileUpload ID="CV" runat="server" /><br />
    >
    > &nbsp;<asp:Button ID="Button1" runat="server" CommandName="Insert" Text="Button" />
    >
    > </InsertItemTemplate>
    >
    > </asp:FormView>
    >
    >
    > </div>
    >
    > <asp:ObjectDataSource ID="objdsCustomer" runat="server" InsertMethod="createcustomer"
    >
    > SelectMethod="getcustomer_list" TypeName="Customer">
    >
    > <InsertParameters>
    >
    > <asp:parameter Name="strName" Type="Object" />
    >
    > <asp:parameter Name="strEmail" Type="Object" />
    >
    > <asp:parameter Name="CV" Type="Object" />
    >
    > </InsertParameters>
    >
    > </asp:ObjectDataSource>
    >
    >
    >
    >
    >
    > Public Sub createcustomer(ByVal strname As String, ByVal strEmail As String, ByVal cv() As Byte)
    >
    > End Sub
    >
    >
     
    Jon Paal [MSMD], Mar 31, 2007
    #2
    1. Advertising

  3. Chris

    Chris Guest

    I 've got something like that working. My problem is that I want to put all
    logic, and that would include manipulating the uploaded file in a business
    object, preferably one method. Putting that code on the page with the
    control doesn't seem very 3 tier, which is what I was aiming for. Really I
    want to grab the stream rather than file the control.

    I got something like this working ages ago but I've lost the code. I seem to
    remember being able to specify the type as filebytes though.

    "Jon Paal [MSMD]" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    news:...
    > some sample code here may help
    >
    > http://www.superexpert.com/Books/AspNet2Unleashed/Chapters/Chapter11/Listing11_26.aspx
    > http://vinayakshrestha.wordpress.com/2007/03/31/ajax-extensions-file-upload-workaround/
    >
    > general formview info is here:
    >
    > http://msconline.maconstate.edu/tutorials/ASPNET20/default.htm
    >
    >
    >
    >
    > "Chris" <> wrote in message
    > news:...
    >>I want do my insert to a database using a formview bound to an
    >>objectdatasource. I also want to upload a file. I'm quite new to and this
    >>is just test code but Is there any way I can pass the file as a byte array
    >>and manipulate it in my business object. Currently what I have is this,
    >>but my byte array in my business object is nothing. It's like it's not
    >>passed the file as a stream. I think I am not binding the FileUpload to
    >>the insertparameter but I don't know how to fix it. Regards, Chris.
    >>
    >>
    >> <asp:FormView ID="frmvwCustomer" runat="server"
    >> DataSourceID="objdsCustomer" DefaultMode="Insert" AllowPaging="True">
    >>
    >> <InsertItemTemplate>
    >>
    >> Name<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
    >>
    >> Email<asp:TextBox ID="txtemail" Text="<%# Bind('strEmail') %>"
    >> runat="server"></asp:TextBox><br />
    >>
    >> CV<asp:FileUpload ID="CV" runat="server" /><br />
    >>
    >> &nbsp;<asp:Button ID="Button1" runat="server" CommandName="Insert"
    >> Text="Button" />
    >>
    >> </InsertItemTemplate>
    >>
    >> </asp:FormView>
    >>
    >>
    >> </div>
    >>
    >> <asp:ObjectDataSource ID="objdsCustomer" runat="server"
    >> InsertMethod="createcustomer"
    >>
    >> SelectMethod="getcustomer_list" TypeName="Customer">
    >>
    >> <InsertParameters>
    >>
    >> <asp:parameter Name="strName" Type="Object" />
    >>
    >> <asp:parameter Name="strEmail" Type="Object" />
    >>
    >> <asp:parameter Name="CV" Type="Object" />
    >>
    >> </InsertParameters>
    >>
    >> </asp:ObjectDataSource>
    >>
    >>
    >>
    >>
    >>
    >> Public Sub createcustomer(ByVal strname As String, ByVal strEmail As
    >> String, ByVal cv() As Byte)
    >>
    >> End Sub
    >>
    >>

    >
    >
     
    Chris, Mar 31, 2007
    #3
  4. I may be missing your objective, but it seems that a formview, and associated upload control, would be dealt with through an event
    handler (code beside or in page) rather than a business object layer...
     
    Jon Paal [MSMD], Mar 31, 2007
    #4
  5. Chris

    Chris Guest

    What I mean is that I don't want to put business logic about e.g. where an
    object is saved in the presentation layer. If this is something that you
    can't really acheive with the formview/objectdatasorce I have a work around
    it's not very good but I have posted it. A cleaner solution is to bind the
    the stream to byte array and use that in the business layer.

    My slightly hacky work around is to tell pass the business object the name
    of the formview and fileupload and it find it on the page. It works but I
    don't like it....



    <form id="form1" runat="server" enctype="multipart/form-data">
    <div>
    &nbsp;<asp:FormView ID="frmvwCustomer" runat="server"
    DataSourceID="objdsCustomer" DefaultMode="Insert" AllowPaging="True">
    <InsertItemTemplate>
    Name<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
    Email<asp:TextBox ID="txtemail" Text="<%# Bind('strEmail') %>"
    runat="server"></asp:TextBox><br />
    CV<asp:FileUpload ID="CV" runat="server" /><br />
    &nbsp;<asp:Button ID="Button1" runat="server" CommandName="Insert"
    Text="Button" />
    </InsertItemTemplate>
    </asp:FormView>
    </div>
    <asp:ObjectDataSource ID="objdsCustomer" runat="server"
    InsertMethod="createcustomer"
    SelectMethod="getcustomer_list" TypeName="Customer">
    <InsertParameters>
    <asp:parameter Name="strname" Type="String" />
    <asp:parameter Name="strEmail" Type="String" />
    <asp:parameter Name="formview" DefaultValue="frmvwCustomer" Type="String" />
    <asp:parameter Name="upload" DefaultValue="CV" Type="String" />
    </InsertParameters>
    </asp:ObjectDataSource>
    </form>


    Public Sub createcustomer(ByVal strname As String, ByVal strEmail As String,
    ByVal formview As String, ByVal upload As String)
    Dim page As Page = HttpContext.Current.CurrentHandler
    Dim frmvw As FormView = CType(page.FindControl(formview), FormView)
    Dim fileup As FileUpload = CType(frmvw.FindControl(upload), FileUpload)
    Dim cv() As Byte = fileup.FileBytes
    HttpContext.Current.Response.Write(cv)
    HttpContext.Current.Response.ContentType = "application/pdf"
    HttpContext.Current.Response.AddHeader("Content-Disposition",
    "attachment;filename = test.pdf")
    HttpContext.Current.Response.BinaryWrite(cv)
    HttpContext.Current.Response.End()
    End Sub



    "Jon Paal [MSMD]" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    news:%...
    >I may be missing your objective, but it seems that a formview, and
    >associated upload control, would be dealt with through an event handler
    >(code beside or in page) rather than a business object layer...
    >
    >
    >
     
    Chris, Mar 31, 2007
    #5
  6. maybe this will help you as an example of a helper class...

    http://weblibraries.sourceforge.net/main/sharpupload_proj.html


    "Chris" <> wrote in message news:%...
    > What I mean is that I don't want to put business logic about e.g. where an object is saved in the presentation layer. If this is
    > something that you can't really acheive with the formview/objectdatasorce I have a work around it's not very good but I have
    > posted it. A cleaner solution is to bind the the stream to byte array and use that in the business layer.
    >
    > My slightly hacky work around is to tell pass the business object the name of the formview and fileupload and it find it on the
    > page. It works but I don't like it....
    >
    >
    >
    > <form id="form1" runat="server" enctype="multipart/form-data">
    > <div>
    > &nbsp;<asp:FormView ID="frmvwCustomer" runat="server" DataSourceID="objdsCustomer" DefaultMode="Insert" AllowPaging="True">
    > <InsertItemTemplate>
    > Name<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
    > Email<asp:TextBox ID="txtemail" Text="<%# Bind('strEmail') %>" runat="server"></asp:TextBox><br />
    > CV<asp:FileUpload ID="CV" runat="server" /><br />
    > &nbsp;<asp:Button ID="Button1" runat="server" CommandName="Insert" Text="Button" />
    > </InsertItemTemplate>
    > </asp:FormView>
    > </div>
    > <asp:ObjectDataSource ID="objdsCustomer" runat="server" InsertMethod="createcustomer"
    > SelectMethod="getcustomer_list" TypeName="Customer">
    > <InsertParameters>
    > <asp:parameter Name="strname" Type="String" />
    > <asp:parameter Name="strEmail" Type="String" />
    > <asp:parameter Name="formview" DefaultValue="frmvwCustomer" Type="String" />
    > <asp:parameter Name="upload" DefaultValue="CV" Type="String" />
    > </InsertParameters>
    > </asp:ObjectDataSource>
    > </form>
    >
    >
    > Public Sub createcustomer(ByVal strname As String, ByVal strEmail As String, ByVal formview As String, ByVal upload As String)
    > Dim page As Page = HttpContext.Current.CurrentHandler
    > Dim frmvw As FormView = CType(page.FindControl(formview), FormView)
    > Dim fileup As FileUpload = CType(frmvw.FindControl(upload), FileUpload)
    > Dim cv() As Byte = fileup.FileBytes
    > HttpContext.Current.Response.Write(cv)
    > HttpContext.Current.Response.ContentType = "application/pdf"
    > HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename = test.pdf")
    > HttpContext.Current.Response.BinaryWrite(cv)
    > HttpContext.Current.Response.End()
    > End Sub
    >
    >
    >
    > "Jon Paal [MSMD]" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:%...
    >>I may be missing your objective, but it seems that a formview, and associated upload control, would be dealt with through an
    >>event handler (code beside or in page) rather than a business object layer...
    >>
    >>
    >>

    >
    >
     
    Jon Paal [MSMD], Mar 31, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    0
    Views:
    1,178
    Chris
    May 9, 2006
  2. Islamegy®

    FormView and FileUpload

    Islamegy®, May 17, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    7,481
    Islamegy®
    May 18, 2006
  3. marygfrc

    Formview error with fileupload

    marygfrc, Jun 15, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    392
    marygfrc
    Jun 15, 2006
  4. RosH
    Replies:
    0
    Views:
    457
  5. =?Utf-8?B?Z292ZXI=?=

    FileUpload Control in FormView using ObjectDataSource

    =?Utf-8?B?Z292ZXI=?=, Sep 2, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    755
    =?Utf-8?B?Z292ZXI=?=
    Sep 2, 2007
Loading...

Share This Page