Question on error message "Object must implement IConvertible"

Posted on 2006-05-21
Medium Priority
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
  • 2
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16731617
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

prakash_prk earned 2000 total points
ID: 16731638
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[]


Author Comment

ID: 16734082
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

ID: 16734164
yes thats correct.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses

864 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