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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
JezmondCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JezmondCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.