ASP.Net FileUpload PostedFile gives NULL Reference Exception

Last Reply on Jul 11, 2012 03:06 AM By Mudassar

Posted on Jul 09, 2012 11:14 PM

hi 

this is my button event code

 

 

 

 

 

  protected void ImageButton2_Click1(object sender, ImageClickEventArgs e)
    {
        string data = Server.UrlDecode(Request.QueryString["BehCode3"]);
        string filename = System.IO.Path.GetFileName(fup1.PostedFile.FileName);
        string filename1 = System.IO.Path.GetFileName(fup2.PostedFile.FileName);
        string filename2 = System.IO.Path.GetFileName(fup3.PostedFile.FileName);
       

        string price = RadioButton2.Checked ? TextBox1.Text : "noprice";
        SqlCommand _cmd = new SqlCommand("insertproduct", _cn);
        _cmd.CommandType = CommandType.StoredProcedure;
        _cn.Open();

        _cmd.Parameters.AddWithValue("@Name", txtname.Text);
        _cmd.Parameters.AddWithValue("@Model", txtmodel.Text);
        _cmd.Parameters.AddWithValue("@Description", CKEditorControl1.Text);
        _cmd.Parameters.AddWithValue("@Price", price);

        _cmd.Parameters.AddWithValue("@Classification", DDL1.SelectedItem.Text);
        _cmd.Parameters.AddWithValue("@subset", DDL1.SelectedItem.Text);
        _cmd.Parameters.AddWithValue("@behcode", data);
        _cmd.Parameters.AddWithValue("@image", filename);
        _cmd.Parameters.AddWithValue("@image1", filename1);
        _cmd.Parameters.AddWithValue("@image2", filename2);
        _cmd.ExecuteNonQuery();

        _cn.Close();
        Response.Redirect(Request.Url.AbsoluteUri);
    }


when i click on button  this error occur

 

  protected void ImageButton2_Click1(object sender, ImageClickEventArgs e)

    {

        string data = Server.UrlDecode(Request.QueryString["BehCode3"]);

        string filename = System.IO.Path.GetFileName(fup1.PostedFile.FileName);

        string filename1 = System.IO.Path.GetFileName(fup2.PostedFile.FileName);

        string filename2 = System.IO.Path.GetFileName(fup3.PostedFile.FileName);

       

 

        string price = RadioButton2.Checked ? TextBox1.Text : "noprice";

        SqlCommand _cmd = new SqlCommand("insertproduct", _cn);

        _cmd.CommandType = CommandType.StoredProcedure;

        _cn.Open();

 

        _cmd.Parameters.AddWithValue("@Name", txtname.Text);

        _cmd.Parameters.AddWithValue("@Model", txtmodel.Text);

        _cmd.Parameters.AddWithValue("@Description", CKEditorControl1.Text);

        _cmd.Parameters.AddWithValue("@Price", price);

 

        _cmd.Parameters.AddWithValue("@Classification", DDL1.SelectedItem.Text);

        _cmd.Parameters.AddWithValue("@subset", DDL1.SelectedItem.Text);

        _cmd.Parameters.AddWithValue("@behcode", data);

        _cmd.Parameters.AddWithValue("@image", filename);

        _cmd.Parameters.AddWithValue("@image1", filename1);

        _cmd.Parameters.AddWithValue("@image2", filename2);

        _cmd.ExecuteNonQuery();

 

        _cn.Close();

        Response.Redirect(Request.Url.AbsoluteUri);

    }

 

 

 

what should i do?

 

thanks

 

Posted on Jul 11, 2012 03:06 AM

You are totally in wrong directions and I think you need to learn some basics of APS.Net

1. FileUpload file is available only on one button click. the second time you click button the File will be lost

2. Even if the FileUpload is in UpdatePanel the File will be lost.

So you cannot do your processing in 2 buttons. You will have to do it on 1 button only.