That's because the FileUpload control does not save the file to disk - it doesn't know what you want to do with it.
Here is the code I use:
private string SaveUpload(FileUpload fl)
{
if (fl.HasFile)
{
try
{
int version = 0;
string filename = Path.GetFileName(fl.FileName);
byte[] filedata = fl.FileBytes;
string strCon = ConnectionStrings.Download;
using (SqlConnection con = new SqlConnection(strCon))
{
con.Open();
using (SqlCommand ins = new SqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version, uploadedOn) " +
"VALUES (@ID, @FN, @DS, @DT, @VS, @UD)", con))
{
ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
ins.Parameters.AddWithValue("@FN", filename);
ins.Parameters.AddWithValue("@DS", "");
ins.Parameters.AddWithValue("@DT", filedata);
ins.Parameters.AddWithValue("@VS", version);
ins.Parameters.AddWithValue("@UD", DateTime.Now);
ins.ExecuteNonQuery();
}
}
return string.Format("{0} uploaded, version = {1}", filename, version);
}
catch (Exception ex)
{
return "The file could not be uploaded. The following error occured: " + ex.Message;
}
}
return "Please select a file.";
}
That gets called from the Upload Button Click event handler.