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. RequiredFi eldValidat or RequiredFieldValidator1;
protected System.Web.UI.HtmlControls .HtmlInput File 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.b tnUpload_C lick);
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
private void btnUpload_Click(object sender, System.EventArgs e)
{
if (Page.IsValid) // Save file
{
Stream fileStream = UploadFile.PostedFile.Inpu tStream;
int fileLen = UploadFile.PostedFile.Cont entLength;
string fileType = UploadFile.PostedFile.Cont entType;
string fileName = txtFilePath.Text;
byte[] fileBinaryData = new byte[fileLen];
int n = fileStream.Read(fileBinary Data, 0, fileLen);
string idis = Request.QueryString["id"];
int busid = System.Convert.ToInt32(idi s);
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(Configuratio nSettings. AppSetting s["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(par am0);
SqlParameter param1 = new SqlParameter("@DocData", SqlDbType.NText, 50);
param1.Value = fileBin;
command.Parameters.Add(par am1);
SqlParameter param2 = new SqlParameter("@DataType", SqlDbType.VarChar, 50);
param2.Value = fileContenttype;
command.Parameters.Add(par am2);
SqlParameter param3 = new SqlParameter("@busid", SqlDbType.Int, 4);
param3.Value = busid;
command.Parameters.Add(par am3);
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;
}
}
}
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.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.HtmlControls
protected System.Web.UI.WebControls.
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.b
this.Load += new System.EventHandler(this.P
}
#endregion
private void btnUpload_Click(object sender, System.EventArgs e)
{
if (Page.IsValid) // Save file
{
Stream fileStream = UploadFile.PostedFile.Inpu
int fileLen = UploadFile.PostedFile.Cont
string fileType = UploadFile.PostedFile.Cont
string fileName = txtFilePath.Text;
byte[] fileBinaryData = new byte[fileLen];
int n = fileStream.Read(fileBinary
string idis = Request.QueryString["id"];
int busid = System.Convert.ToInt32(idi
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(Configuratio
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(par
SqlParameter param1 = new SqlParameter("@DocData", SqlDbType.NText, 50);
param1.Value = fileBin;
command.Parameters.Add(par
SqlParameter param2 = new SqlParameter("@DataType", SqlDbType.VarChar, 50);
param2.Value = fileContenttype;
command.Parameters.Add(par
SqlParameter param3 = new SqlParameter("@busid", SqlDbType.Int, 4);
param3.Value = busid;
command.Parameters.Add(par
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
return numRowsAffected;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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.
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.