View Single Post
  #1 (permalink)  
Old July 25th, 2011, 12:24 PM
dmacklin2010 dmacklin2010 is offline
Registered User
Join Date: Jul 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validate File Format of File Upload to Database Not Working in Codefile in C#...

Created online form in C# that uploads a document to SQL database (this is the way I want it for now since I am learning), and checks to make sure uploaded file is of format .doc, .docx, .pdf or .rtf. Currently the C# codefile (see below section in red) will not get past the section that reads "Error - Your resume format is not .doc, .docx, .pdf or .rtf. Please resubmit the form to attempt your upload again." which verifies file format of document. I can't determine if my logic is incorrect or something else. I also removed the period in from of the .doc, .docx, .pdf and .rtf in the mentioned section that is causing a problem, but I still cannot resolve the problem. New to and C#, and need to have someone review my code to see what problem is:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;

namespace SampleNameSpaceName
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)


protected void btn_Upload_Click(object sender, EventArgs e)
if (FileToUpload.PostedFile == null || String.IsNullOrEmpty(FileToUpload.PostedFile.FileName) || FileToUpload.PostedFile.InputStream == null)
lit_Status.Text = "<center><font color='0000ff'><br />Error - unable to upload file. Please try again.<br /></font></center>";
else if (FileToUpload.HasFile)
string extension = System.IO.Path.GetExtension(FileToUpload.FileName).ToLower();
if (extension != ".doc" || extension != ".docx" || extension != ".pdf" || extension != ".rtf")
lit_Status.Text = "<center><font color='0000ff'>Error - Your resume format is not .doc, .docx, .pdf or .rtf. Please resubmit the form to attempt your upload again.<br /><br /></font></center>";
using (SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MySampleEventConnectionString"].ConnectionString))
const string SQL = "INSERT INTO SampleDataTable ([firstname], [initial], [lastname], [studentid], [phone], [email], [textnotes], [MIME], [BinaryData], [DateTimeUploaded]) VALUES (@firstname, @initial, @lastname, @studentid, @phone, @email, @textnotes, @MIME, @BinaryData, @DateTimeUploaded)";
SqlCommand cmd = new SqlCommand(SQL, Conn);
cmd.Parameters.AddWithValue("@firstname", FirstName.Text.Trim());
cmd.Parameters.AddWithValue("@initial", Initial.Text.Trim());
cmd.Parameters.AddWithValue("@lastname", LastName.Text.Trim());
cmd.Parameters.AddWithValue("@studentid", StudentId.Text.Trim());
cmd.Parameters.AddWithValue("@phone", Phone.Text.Trim());
cmd.Parameters.AddWithValue("@email", Email.Text.Trim());
cmd.Parameters.AddWithValue("@textnotes", textNotes.Text.Trim());
cmd.Parameters.AddWithValue("@MIME", FileToUpload.PostedFile.ContentType);

byte[] imageBytes = new byte[FileToUpload.PostedFile.InputStream.Length + 1];
FileToUpload.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
cmd.Parameters.AddWithValue("@BinaryData", imageBytes);
cmd.Parameters.AddWithValue("@DateTimeUploaded", DateTime.Now);

lit_Status.Text = "<center><font color='ff0000'><br />File successfully uploaded - thank you.<br /></font></center>";
catch (Exception ex)

Last edited by dmacklin2010; July 25th, 2011 at 12:27 PM..