ObjectDataSource and FileUpload in App_Code?

Hi, I'm trying to create a class in App_Code that can upload a file and save the path to DB (SQL Server). I've found this VB-code...
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_22923840.html
But it doesn't tell much about the parameters.

I get the error: "Object reference not set to an instance of an object". And "if (movie.HasFile)" is highlighted.
What should i do?
/// App_Code ///
 
public class PageDetails
{
    public PageDetails()
    {
    }
 
    public void InsertDetailsView(String name, FileUpload movie)
    {
        if (movie.HasFile)
        {
            String connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectionString);
 
            movie.SaveAs(Request.PhysicalApplicationPath.ToString() + "movie\\" + movie.FileName);
            
            SqlCommand cmd = new SqlCommand("INSERT INTO pages ([name],[movie]) VALUES (@name,@movie)", conn);
 
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@movie", movie);
 
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
}
 
 
 
/// aspx ///
 
<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Insert"
    DataKeyNames="ID" AutoGenerateRows="False" DataSourceID="odsDetailsView" 
    Height="50px" Width="500px">
    <Fields>
        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="False"/>
        <asp:BoundField DataField="name" HeaderText="Titel" SortExpression="name" />
        
        <asp:TemplateField HeaderText="Film" SortExpression="movie">
            <InsertItemTemplate>
                <asp:FileUpload ID="movie" runat="server" />
            </InsertItemTemplate>
        </asp:TemplateField>
 
        <asp:CommandField ShowInsertButton="True" ButtonType="Button" />
        <asp:CommandField ShowCancelButton="True" ButtonType="Button" />
    </Fields>
</asp:DetailsView>
 
<asp:ObjectDataSource ID="odsDetailsView" runat="server" TypeName="PageDetails"
    InsertMethod="InsertDetailsView">
    <InsertParameters>
        <asp:Parameter Name="name" Type="String" />
        <asp:Parameter Name="movie" Type="Object" />
    </InsertParameters>
</asp:ObjectDataSource>

Open in new window

BalltownAsked:
Who is Participating?
 
JezmondConnect With a Mentor Commented:
Still think it permissions ;)

Anyhow.
1) I created a brand new c# asp.net web site with a app_code folder.
2) I create a simple class like yours without the sql code in the app_code dir:
public class Class1
{
    public void InsertDetailsView(String name, FileUpload movie)
    {
        if (movie.HasFile)
        {
            movie.SaveAs(@"c:\temp\" + movie.FileName);
        }
    }
}
3) I added a fileupload control and a button to the aspx page.
4) I added the following code to the button event:
    protected void Button1_Click(object sender, EventArgs e)
    {
        Class1 class1 = new Class1();
        class1.InsertDetailsView("wibble", this.FileUpload1);
    }
5) Ran the code, with no problems. any file a choose end up in temp.
0
 
JezmondCommented:
Firstly.

Check the permissions on the folder you are uploading to, it may need ASP.NET (or whatever the account is that invokes the process) account to have read/write permissions
0
 
BalltownAuthor Commented:
Thanks, Jezmond. I checked the permissions for the upload folder and they look correct. But even if I outcomment the line:

movie.SaveAs(Request.PhysicalApplicationPath.ToString() + "movie\\" + movie.FileName); // DateTime.Now.ToString()

... I still get the same error. Also, i did a fileupload in aspx.cs which worked. It's more like: how do i get it to work in the App_Code class-file?
0
 
JezmondConnect With a Mentor Commented:
Sorry i was wrong this is your answer, i didnt pay attention the fact that your control was within another control.
in the aspx:
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        Class1 class1 = new Class1();

        FileUpload movie = DetailsView1.FindControl("movie") as FileUpload;

        class1.InsertDetailsView("wibble", movie);
    }
0
 
BalltownAuthor Commented:
Your code did the trick... So it didn't have to do with permissions ;-)

Also, what was wrong in my aspx was that I put DataSourceID and DataKeyNames in my DetailsView-tag... they shouldn't be there in this case.

Thanks, Jezmond
0
All Courses

From novice to tech pro — start learning today.