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. General Programming
  3. C#
  4. how to convert a object to a byte array byte[]

how to convert a object to a byte array byte[]

Scheduled Pinned Locked Moved C#
databasedata-structureshelptutorialquestion
3 Posts 3 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.
  • W Offline
    W Offline
    we_swati
    wrote on last edited by
    #1

    HI My problem is that I am reading a image field from the database table . then I need to Convert that image field value into a byte[] . dread = sqlcomm.ExecuteReader(); sqlcomm = new SqlCommand("getCont1" ,sqlconn); sqlcomm.CommandType = CommandType.StoredProcedure; dread = sqlcomm.ExecuteReader(); while ( dread.Read()) { temp= (dread.GetValue(0)); } this "temp" var store the image field value as object . now i need to convert this object into byte[] as i have to pass it to a function which take byte[] as parameter. what I need to do ? thanks ss thanks

    T F 2 Replies Last reply
    0
    • W we_swati

      HI My problem is that I am reading a image field from the database table . then I need to Convert that image field value into a byte[] . dread = sqlcomm.ExecuteReader(); sqlcomm = new SqlCommand("getCont1" ,sqlconn); sqlcomm.CommandType = CommandType.StoredProcedure; dread = sqlcomm.ExecuteReader(); while ( dread.Read()) { temp= (dread.GetValue(0)); } this "temp" var store the image field value as object . now i need to convert this object into byte[] as i have to pass it to a function which take byte[] as parameter. what I need to do ? thanks ss thanks

      T Offline
      T Offline
      turbochimp
      wrote on last edited by
      #2

      An image SQL server column type translates to a byte array (byte[])framework type. In your example, the 'temp' variable would have to be declared as byte[]. You could also use the GetBytes(int index) method on your reader. For a more complete type mapping table, look here[^].

      The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

      1 Reply Last reply
      0
      • W we_swati

        HI My problem is that I am reading a image field from the database table . then I need to Convert that image field value into a byte[] . dread = sqlcomm.ExecuteReader(); sqlcomm = new SqlCommand("getCont1" ,sqlconn); sqlcomm.CommandType = CommandType.StoredProcedure; dread = sqlcomm.ExecuteReader(); while ( dread.Read()) { temp= (dread.GetValue(0)); } this "temp" var store the image field value as object . now i need to convert this object into byte[] as i have to pass it to a function which take byte[] as parameter. what I need to do ? thanks ss thanks

        F Offline
        F Offline
        felopater
        wrote on last edited by
        #3

        if you want to convert it to byte array you have to serialize it. using System.Runtime.Serialization.Formatters.Binary; /*Note i suppose your object is serializable if not add [Serializable] attrubite to your object class*/ BinaryFormatter Serialize=new BinaryFormatter(); MemoryStream stream=new MemoryStream(); byte[]ObjectInBytes=Serialize.Serialize(stream,temp); //to Deserialize stream=new MemoryStream(ObjectInBytes); stream.Seek(0,SeekOrigin.Begin); temp=(TypeOfYourObject) Serialize.Deserialize(stream); marcoryos

        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