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. Storing and retrieving objects inside databse

Storing and retrieving objects inside databse

Scheduled Pinned Locked Moved C#
databasesql-serversysadminquestion
5 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.
  • S Offline
    S Offline
    Sabry1905
    wrote on last edited by
    #1

    Hi i want to store and retrieve objects of any kind to SQL server database, i did the following, inside the database table, i had created a field of type varbinary(MAX), and in the code, i had made the following to store the object

    object treatment = dataGridViewDrugList.Rows;
    MemoryStream memStream = new MemoryStream();
    StreamWriter sw = new StreamWriter(memStream);
    sw.Write(treatment);
    dataRow[0] = memStream.GetBuffer();

    and it was successful, now i am not able to retrieve the object back any ideas?

    L 1 Reply Last reply
    0
    • S Sabry1905

      Hi i want to store and retrieve objects of any kind to SQL server database, i did the following, inside the database table, i had created a field of type varbinary(MAX), and in the code, i had made the following to store the object

      object treatment = dataGridViewDrugList.Rows;
      MemoryStream memStream = new MemoryStream();
      StreamWriter sw = new StreamWriter(memStream);
      sw.Write(treatment);
      dataRow[0] = memStream.GetBuffer();

      and it was successful, now i am not able to retrieve the object back any ideas?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Try to 'serialize' the object, as opposed to simply trying to wildly dump it's contents. There's an example using the XmlSerializer on MSDN; once it works, you replace the XmlSerializer with a BinaryFormatter and you're done.

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      S 1 Reply Last reply
      0
      • L Lost User

        Try to 'serialize' the object, as opposed to simply trying to wildly dump it's contents. There's an example using the XmlSerializer on MSDN; once it works, you replace the XmlSerializer with a BinaryFormatter and you're done.

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        S Offline
        S Offline
        Sabry1905
        wrote on last edited by
        #3

        please send the link

        L 1 Reply Last reply
        0
        • S Sabry1905

          please send the link

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Here[^] it is. Serialization may take some getting-used-to, but it's more versatile than dumping memory. I'd recommend building it in two separate steps; First, create a small console-app to serialize a random object. Once that works, you should be able to switch between Xml-serialization and binary with ease. Second part would be to store that "information" in a database. That will work the same way for any information; if you get stuck once you're there, simply come back and we'll look at it again :)

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          S 1 Reply Last reply
          0
          • L Lost User

            Here[^] it is. Serialization may take some getting-used-to, but it's more versatile than dumping memory. I'd recommend building it in two separate steps; First, create a small console-app to serialize a random object. Once that works, you should be able to switch between Xml-serialization and binary with ease. Second part would be to store that "information" in a database. That will work the same way for any information; if you get stuck once you're there, simply come back and we'll look at it again :)

            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

            S Offline
            S Offline
            Sabry1905
            wrote on last edited by
            #5

            Thanks, i solved the issue, my error was that i was trying to serialize an object of type DataGridViewRowCollection which is not marked as a Serializable object, i made my custom class which marked as Serializable, i had stored all the data in the DataGridViewRowCollection in that class, now everything is OK thanks again,

            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