Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hello,

I have the following code setup to upload files into my database table. I am currently struggling with uploading file type of ".pst". If I try to upload 'ta@xxx.com.pst' and click the upload button, it goes to 'this is page is not available'. The code seems to work on any file type except .pst. I am using chrome/firefox web browser on window 8, to test this code.

C#
protected void btnUpload_Click(object sender, EventArgs e)
{
    //Check whether FileUpload control has file
    if (FileUpload1.HasFile)
    {
        string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
        string documentType = string.Empty;

        //provide document type based on it's extension
        switch (fileExtension)
        {
            case ".pst":
                documentType = "application/pst";
                break;
            case ".pdf":
                documentType = "application/pdf";
                break;
            case ".xls":
                documentType = "application/vnd.ms-excel";
                break;
            case ".xlsx":
                documentType = "application/vnd.ms-excel";
                break;
            case ".jpg":
                documentType = "image/jpg";
                break;
        }

        //Calculate size of file to be uploaded
        int fileSize = FileUpload1.PostedFile.ContentLength;

        //Create array and read the file into it
        byte[] documentBinary = new byte[fileSize];
        FileUpload1.PostedFile.InputStream.Read(documentBinary, 0, fileSize);

        // Create SQL Connection
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        // Create SQL Command and Sql Parameters
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "INSERT INTO SaveDoc(DocName,Type,DocData)" +
                          " VALUES (@DocName,@Type,@DocData)";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        SqlParameter DocName = new SqlParameter("@DocName", SqlDbType.VarChar, 50);
        DocName.Value = fileName.ToString();
        cmd.Parameters.Add(DocName);

        SqlParameter Type = new SqlParameter("@Type", SqlDbType.VarChar, 50);
        Type.Value = documentType.ToString();
        cmd.Parameters.Add(Type);

        SqlParameter uploadedDocument = new SqlParameter("@DocData", SqlDbType.Binary,fileSize);
        uploadedDocument.Value = documentBinary;
        cmd.Parameters.Add(uploadedDocument);

        con.Open();
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result > 0)
            lblMessage.Text = "File saved to database";
        GridView1.DataBind();
    }
}


ASP.NET
<div>
<asp:FileUpload ID="FileUpload1" runat="server"/>
<asp:Button ID="btnUpload" runat="server"  
            onclick="btnUpload_Click" 
            Text="Upload"/>
</div>



If I am missing something in the code, could you please notify me.
Many thanks for your time and help.
Posted
Updated 20-Jan-14 0:33am
v2
Comments
bowlturner 20-Jan-14 9:09am    
My first guess would be you don't have read/write access to the file you are trying to copy.
miss786 21-Jan-14 9:57am    
Dear all, Thank you all so much for your response. I am currently working through my code following your suggestion to get this issue solves. You all have been a great help. Many thanks.

Your code (possibly) crashes at
int result = cmd.ExecuteNonQuery();


If you enclose your code with 'try catch' structure, and add
C#
lblMessage.Text=ex.Message;
in the catch block, you can see your exception.

Some advices:
1- design multitier application (design a separate class for data management)
2- enclose your UI handling codes in a 'try catch' structure to avoid 'unhandled exception' and a total crash.
3- use the debugger to trace your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900