Link to home
Start Free TrialLog in
Avatar of chuang4630
chuang4630

asked on

Question on error message "Object must implement IConvertible"

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.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <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);

            }
            #endregion

            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.");
                        }
                        else
                        {
                              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;
                  command.Parameters.Add(param0);

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

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

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

                  connection.Open();
                  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.
                  connection.Close();

                  return numRowsAffected;
            }
      }
}
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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:
http://www.chilkatsoft.com/faq/DotNetStrToBytes.html

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.
ASKER CERTIFIED SOLUTION
Avatar of prakash_prk
prakash_prk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chuang4630
chuang4630

ASKER

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?

yes thats correct.