Click here to Skip to main content
15,884,388 members

ASP.Net file upload to sql server 2008 Database

jaipal0908 asked:

Open original thread
Hi All,

I need a sample application for File Upload to SQL satabase.
I am using asp.net c#.net and sql server.

I am getting an error in Utilities class like this.
Please can anyone help me.
XML
Server Error in '/' Application.
Incorrect syntax near '@Document'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '@Document'.

Source Error:

Line 68:                 cmd.Parameters["@Size"].Value = size;
Line 69:                 cmd.Parameters["@Document"].Value = fileData;
Line 70:                 cmd.ExecuteNonQuery();
Line 71: 
Line 72:                 connection.Close();


Source File: C:\Users\CBS\documents\visual studio 2010\Projects\Mcases\Mcases\Utilities.cs    Line: 70

Stack Trace:



Here is utilities.cs class
C#
public static void SaveFile(string name, string contentType, int size, byte[] fileData)
       {
           using (SqlConnection connection = new SqlConnection())
           {
               OpenConnection(connection);
               SqlCommand cmd = new SqlCommand();
               cmd.Connection = connection;
               cmd.CommandTimeout = 0;

               string commandText = "INSERT INTO Document VALUES(@FileName, @ContentType,@Size,@Document)";
               commandText = commandText + "@Document";
               cmd.CommandText = commandText;
               cmd.CommandType = CommandType.Text;

               cmd.Parameters.Add("@FileName", SqlDbType.VarChar);
               cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50);
               cmd.Parameters.Add("@size", SqlDbType.Int);
               cmd.Parameters.Add("@Document", SqlDbType.VarBinary);

               cmd.Parameters["@FileName"].Value = name;
               cmd.Parameters["@ContentType"].Value = contentType;
               cmd.Parameters["@Size"].Value = size;
               cmd.Parameters["@Document"].Value = fileData;
               cmd.ExecuteNonQuery();

               connection.Close();
           }
       }


And this is my button click code to save file in Database

C#
protected void btnUpload_Click(object sender, EventArgs e)
       {
           HttpFileCollection files = Request.Files;
           foreach (string fileTagName in files)
           {
               HttpPostedFile file = Request.Files[fileTagName];
               if (file.ContentLength > 0)
               {
                   // Due to the limit of the max for a int type, the largest file can be
                   // uploaded is 2147483647, which is very large anyway.
                   int size = file.ContentLength;
                   string name = file.FileName;
                   int position = name.LastIndexOf("\\");
                   name = name.Substring(position + 1);
                   string contentType = file.ContentType;
                   byte[] fileData = new byte[size];
                   file.InputStream.Read(fileData, 0, size);

                   Utilities.SaveFile(name, contentType, size, fileData);
               }
           }
           }
Tags: C# (C# 4.0), SQL Server, SQL Server 2008, ASP.NET4.0

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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