Unable to cast object of type 'System.Byte[]'to 'System.IConvertible'. in Uploadiong Image in to Sqlserver
-
Hi, This is from Chandrakanth. Now i am uploading Image in to Sqlserver2005. I have written coding like this using Enterprise Library protected void btnUpload_Click(object sender, EventArgs e) { if (upXLTalisma.PostedFile != null) { if (upXLTalisma.PostedFile.ContentLength > 0) { HttpPostedFile objHttpPostedFile = upXLTalisma.PostedFile; if (CheckValidFileType(objHttpPostedFile.FileName)) { int intContentlength = objHttpPostedFile.ContentLength; byte[] bytImage = new byte[intContentlength]; objHttpPostedFile.InputStream.Read(bytImage, 0, intContentlength); KF.Search.StoreImage(Convert.ToByte(bytImage)); } } } } public static bool CheckValidFileType(string strImage) { string strImageFile; strImageFile = strImage; if (strImageFile.LastIndexOf(".") == -1) return (false); int i = strImageFile.LastIndexOf("."); strImageFile = strImageFile.Substring(strImageFile.LastIndexOf(".") + 1); return (strImageFile.ToUpper() == "JPG"); } This is the code i am using in Class file public static void StoreImage(byte strImage) { try { Database objDataBase = DatabaseFactory.CreateDatabase(); DbCommand objDbCommand = objDataBase.GetStoredProcCommand("STP_KF_IMages"); objDataBase.AddInParameter(objDbCommand, "@Image", DbType.Binary, strImage); objDataBase.ExecuteNonQuery(objDbCommand); } catch (Exception Ex) { throw new Exception(Ex.Message.ToString()); } } but i am getting error in this line, KF.Search.StoreImage(Convert.ToByte(bytImage)); and Saying "Unable to cast object of type 'System.Byte[]'to 'System.IConvertible'. " Can any one suggest me what to do for that coding. Thanks And Regards Chandrakanth
Chandrakanth
-
Hi, This is from Chandrakanth. Now i am uploading Image in to Sqlserver2005. I have written coding like this using Enterprise Library protected void btnUpload_Click(object sender, EventArgs e) { if (upXLTalisma.PostedFile != null) { if (upXLTalisma.PostedFile.ContentLength > 0) { HttpPostedFile objHttpPostedFile = upXLTalisma.PostedFile; if (CheckValidFileType(objHttpPostedFile.FileName)) { int intContentlength = objHttpPostedFile.ContentLength; byte[] bytImage = new byte[intContentlength]; objHttpPostedFile.InputStream.Read(bytImage, 0, intContentlength); KF.Search.StoreImage(Convert.ToByte(bytImage)); } } } } public static bool CheckValidFileType(string strImage) { string strImageFile; strImageFile = strImage; if (strImageFile.LastIndexOf(".") == -1) return (false); int i = strImageFile.LastIndexOf("."); strImageFile = strImageFile.Substring(strImageFile.LastIndexOf(".") + 1); return (strImageFile.ToUpper() == "JPG"); } This is the code i am using in Class file public static void StoreImage(byte strImage) { try { Database objDataBase = DatabaseFactory.CreateDatabase(); DbCommand objDbCommand = objDataBase.GetStoredProcCommand("STP_KF_IMages"); objDataBase.AddInParameter(objDbCommand, "@Image", DbType.Binary, strImage); objDataBase.ExecuteNonQuery(objDbCommand); } catch (Exception Ex) { throw new Exception(Ex.Message.ToString()); } } but i am getting error in this line, KF.Search.StoreImage(Convert.ToByte(bytImage)); and Saying "Unable to cast object of type 'System.Byte[]'to 'System.IConvertible'. " Can any one suggest me what to do for that coding. Thanks And Regards Chandrakanth
Chandrakanth
mrgaddam wrote:
objHttpPostedFile.InputStream.Read(bytImage, 0, intContentlength);
Oops! The Read method returns the number of bytes actually read, which can be smaller than the number of bytes requested. If you ignore the return value of the method, you don't know how much of the data that you actually get. You have to get the return value from the method to see how much data was read, and loop until you have read all of the data (you reach content length, or the method returns zero).
mrgaddam wrote:
KF.Search.StoreImage(Convert.ToByte(bytImage));
You can't convert a byte array into a single byte.
mrgaddam wrote:
public static void StoreImage(byte strImage)
This method takes a single byte as parameter. That is truly pointless as you can't store an image in a single byte. Change the method to take an array of bytes. Using hungarian notaion to specify data type is rather pointless in a strongly typed language. And if you specify the wrong type, as you have here when you gave a byte variable a name that says that it's a string, it's only confusing.
Experience is the sum of all the mistakes you have done.
-
mrgaddam wrote:
objHttpPostedFile.InputStream.Read(bytImage, 0, intContentlength);
Oops! The Read method returns the number of bytes actually read, which can be smaller than the number of bytes requested. If you ignore the return value of the method, you don't know how much of the data that you actually get. You have to get the return value from the method to see how much data was read, and loop until you have read all of the data (you reach content length, or the method returns zero).
mrgaddam wrote:
KF.Search.StoreImage(Convert.ToByte(bytImage));
You can't convert a byte array into a single byte.
mrgaddam wrote:
public static void StoreImage(byte strImage)
This method takes a single byte as parameter. That is truly pointless as you can't store an image in a single byte. Change the method to take an array of bytes. Using hungarian notaion to specify data type is rather pointless in a strongly typed language. And if you specify the wrong type, as you have here when you gave a byte variable a name that says that it's a string, it's only confusing.
Experience is the sum of all the mistakes you have done.
-
Hi, Once again this is from Chandrakanth. Can you please tell me what are all things have to follow in Code Thanks and Regards Chandrakanth
Chandrakanth