Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > BOOK: Beginning ASP.NET 2.0 and Databases
|
BOOK: Beginning ASP.NET 2.0 and Databases
This is the forum to discuss the Wrox book Beginning ASP.NET 2.0 and Databases by John Kauffman, Bradley Millington; ISBN: 9780471781349
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 2.0 and Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2011, 12:24 PM
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 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;
}
}
}
}
}
}
}

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I validate file upload? grstad Javascript How-To 30 March 10th, 2007 05:13 PM
upload file in server ( in folder NOT in database) virtual_manu Pro JSP 2 February 20th, 2006 08:39 AM
sample JSP to upload file to database rahulsaxena Pro JSP 2 January 16th, 2006 05:59 AM
Upload a file to sql server database thas123 ASP.NET 1.0 and 1.1 Professional 3 January 9th, 2006 12:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.