Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Unable to cast object of type 'System.Byte[]'to 'System.IConvertible'. in Uploadiong Image in to Sqlserver

Unable to cast object of type 'System.Byte[]'to 'System.IConvertible'. in Uploadiong Image in to Sqlserver

Scheduled Pinned Locked Moved ASP.NET
databasehelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrgaddam
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • M mrgaddam

      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

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      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.

      M 1 Reply Last reply
      0
      • G Guffa

        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.

        M Offline
        M Offline
        mrgaddam
        wrote on last edited by
        #3

        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

        G 1 Reply Last reply
        0
        • M mrgaddam

          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

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          mrgaddam wrote:

          Can you please tell me what are all things have to follow in Code

          I already did that. Read my previous post, and if there is anything that you don't understand, say what it is.

          Experience is the sum of all the mistakes you have done.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups