Click here to Skip to main content
15,896,111 members

File Uploading control in asp.net

Naveen Kumar Malli asked:

Open original thread
When i start my code with file upload control it's all over, but i have to fight with my exception
with exception message of string or binary data would be truncated.I cannot come to know which binary data would be truncated
and also i had created a table of name INFORMATION with columns id,name,photo
query=create table information (id int,name varchar(size),photo varbinary(max))
id=some number
name=name of the uploaded file
photo=content of uploaded file(eg:->JPEG OR PPG..ETC)
and also i had taken one textbox to enter id no and one fileuploadcontrol and one button(upload)
here is my front end code
C#
protected void btnupload_Click(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string contenttype=FileUpload1.PostedFile.ContentType;
    using (Stream fs=FileUpload1.PostedFile.InputStream)
    {
        using (BinaryReader br=new BinaryReader(fs))
        {
         byte[] bt = br.ReadBytes((int)fs.Length);
         string constr = ConfigurationManager.ConnectionStrings["naveen"].ToString();
            using (SqlConnection con=new SqlConnection(constr))
            {
                string query = "insert into information values(@id,@name,@photo)";
                using (SqlCommand cmd=new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.CommandText = query;
                    cmd.Parameters.AddWithValue("@id", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@name", filename);
                    cmd.Parameters.AddWithValue("@photo", contenttype);
                    con.Open();
                    int i=cmd.ExecuteNonQuery();----->Here is the statement which i have got exception
                    con.Close();
                    if (i>0)
                    {
                        Label1.Text = "File uploaded succesfully";
                    }
                    else
                    {
                        Label1.Text = "failed to upload";
                    }
                }
            }

        }
    }
}

and how do i resolve this exception.... :)
Tags: C#, Database Development, Image, File Upload, Binary

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