Question on error message "Object must implement IConvertible"

Posted on 2006-05-21
Last Modified: 2008-01-09
I am trying to upload the .pdf and/or .doc file to SQL Server 2000/2005. First I read the file into to the binary array byte[], then "insert into" the database. The field data type is "ntext". However, I keep getting the error: "Object must implement IConvertible". The answer in this site ( Id did search) does not address my situation. So is there anyone may offer the suggestion. BTW, I am kind of resisting the idea of using "image" field, because of the requirement.
Here are the code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;

namespace FileView
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class UploadImage : System.Web.UI.Page
            protected System.Web.UI.WebControls.Label lblFilePath;
            protected System.Web.UI.WebControls.Button btnUpload;
            protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
            protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;
            protected System.Web.UI.WebControls.TextBox txtFilePath;
            private void Page_Load(object sender, System.EventArgs e)
                  // Put user code to initialize the page here

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
                  this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
                  this.Load += new System.EventHandler(this.Page_Load);


            private void btnUpload_Click(object sender, System.EventArgs e)
                  if (Page.IsValid)      // Save file
                        Stream fileStream = UploadFile.PostedFile.InputStream;
                        int fileLen = UploadFile.PostedFile.ContentLength;
                        string fileType = UploadFile.PostedFile.ContentType;
                        string fileName = txtFilePath.Text;
                        byte[] fileBinaryData = new byte[fileLen];
                        int n = fileStream.Read(fileBinaryData, 0, fileLen);

                        string idis = Request.QueryString["id"];
                        int busid = System.Convert.ToInt32(idis);

                        int RowsAffected = SaveToDB(fileName, fileBinaryData, fileType, busid);
                        if (RowsAffected > 0)
                              Response.Write("<br> The File is saved.");
                              Response.Write("<br> An error occurred uploading the file");

            private int SaveToDB(string fileName, byte[] fileBin, string fileContenttype, int busid)
                  SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
                  SqlCommand command = new SqlCommand("insert into LegalTemplate (DocName,  DocData, DocType, busid) values(@DocName,  @DocData, @DataType, @busid)", connection);

                  SqlParameter param0 = new SqlParameter("@DocName", SqlDbType.VarChar, 50);
                  param0.Value = fileName;

                  SqlParameter param1 = new SqlParameter("@DocData", SqlDbType.NText, 50);
                  param1.Value = fileBin;

                  SqlParameter param2 = new SqlParameter("@DataType", SqlDbType.VarChar, 50);
                  param2.Value = fileContenttype;

                  SqlParameter param3 = new SqlParameter("@busid", SqlDbType.Int, 4);
                  param3.Value = busid;

                  int numRowsAffected = command.ExecuteNonQuery();  // the error is right here.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! The issue is related to the DocData (ntext0 field. If you remove DocData from the command, it works fine.

                  return numRowsAffected;
Question by:chuang4630
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    actually, you try to pass a byte array to the ntext parameter, which is not really compatible.

    you should either transform the byte array into a string:

    or, better, read the string data from the file directly instead of binary data.
    if you really want to read binary data, then the data type ntext is wrong.
    LVL 10

    Accepted Solution

    u can use the following methods.

    change this line to
    param1.Value = Convert.ToBase64String(fileBin);

    u can also change "NText" to "varbinary" datatype and use the byte[]

    LVL 1

    Author Comment

    Thanks prakash_prk, it works now.
    So it looks like @DocData should be a string. I guess that if I implement it as Stored Proc, @DocData should be declared as varbinary, is it right? Or maybe something else?

    LVL 10

    Expert Comment

    yes thats correct.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now