p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Beginning ASP.NET 2.0 and Databases (http://p2p.wrox.com/book-beginning-asp-net-2-0-databases-276/)
-   -   Validate File Format of File Upload to Database Not Working in Codefile in C#... (http://p2p.wrox.com/book-beginning-asp-net-2-0-databases/84473-validate-file-format-file-upload-database-not-working-codefile-c.html)

dmacklin2010 July 25th, 2011 12:24 PM

Validate File Format of File Upload to Database Not Working in Codefile in C#...
 
Created online form in ASP.net 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 ASP.net and C#, and need to have someone review my code to see what problem is:

Code:

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>";
}
else
{
using (SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MySampleEventConnectionString"].ConnectionString))
{
try
{
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);

Conn.Open();
cmd.ExecuteNonQuery();
lit_Status.Text = "<center><font color='ff0000'><br />File successfully uploaded - thank you.<br /></font></center>";
Conn.Close();
}
catch (Exception ex)
{
throw;
}
}
}
}
}
}
}



All times are GMT -4. The time now is 06:35 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.