none
Datarow to bytearray

    Question

  •  

    Hi,

    I want to convert a entire data row to byte array vice versa. How can this be done. My application needs to send data to other application in form of bytes

     

     

    Thanks

    Sanket

    Saturday, November 17, 2007 9:23 AM

Answers

  • I think something along the lines of what VMazur was talking about is what you're looking for. bola shokry's response isn't very scalabe and it will take lots of time to implement.


    You can use this to serialize the data of that datarow.

    Code Block

            public byte[] SerializeDataRow(System.Data.DataRow row)
            {
                if(row == default(System.Data.DataRow))
                    return default(byte[]);

                System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(object[]));
                serializer.Serialize(memStream, row.ItemArray);

                return memStream.ToArray();
            }

    This is how you code deserialize the data.


    Code Block
            public void DeserializeDataRow(System.Data.DataTable table, byte[] rowBytes)
            {
                if (table == default(System.Data.DataTable) || rowBytes == default(byte[]))
                    return;

                System.IO.MemoryStream memStream = new System.IO.MemoryStream(rowBytes);
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(object[]));
                table.Rows.Add((object[])serializer.Deserialize(memStream));
            }

    Tuesday, November 20, 2007 11:39 PM

All replies

  • ItemArray property of the DataRow returns all the values from the DataRow as an array of Objects. When you add row to the DataTable, you could pass that array and new row will be created. But you cannot get result as an array of bytes directly from the DataRow.

     

    Monday, November 19, 2007 11:22 AM
    Moderator
  • Why you say he cannot get result as an array of bytes directly from the DataRow ? !!!
    I think he can use Serialization and deSerialization to convert DataRow to byte array and could be passed between applications , right VMazur?
    Monday, November 19, 2007 5:31 PM
  •  

    I am using MS.Net 2.0 and in this version data row cannot be serialized. It was possible in framework 1.1.

    Can you please tell me any work around for this

     

    Thanks,

    Sanket

    Tuesday, November 20, 2007 9:18 AM
  •  

    You can add the DataRow to a DataTable to create a DataTable with one record and pass this table from application to another, DataTable is Serializable in .Net 2.0
    Tuesday, November 20, 2007 9:36 AM
  •  

    Hi,

    yes i know that way..but i guess a data table object would be heavy as compared to a data row. I am working on a real time application and so performance is very important

     

     

     

    Tuesday, November 20, 2007 10:08 AM
  • hi,

    there is another solution,

    Create your own class that represent the passed DataRow for example MyDataRow which implement

    ISerializable , this class's parameters (local vairables) are the DataRow contents.

    and use MyDataRow object to be sent between applications instead of DataRow object.

    I think this is much better than using DataTable.

    Tuesday, November 20, 2007 11:06 AM
  • Hi,

    Can put any short sample code.. I tried to create the class this way and create object of it.. but it didnt work

     

    <Serializable()> _

    Public Class Class1

    Public dr As DataRow

    End Class

     

     

    Thanks

     

    Tuesday, November 20, 2007 1:34 PM
  •  

    excuse me , i will write code in C#

    and will suppose your DataRow has an integer and a string.

     

    Code Block

    [Serializable]

    public class MyDataRow

    {

    int arg1;           

    string arg2;

    // also create a getter for each argument

     

     

    // constructor to create an instance of MyDataRow using a normal DataRow object

    public MyDataRow(DataRow dataRow)

    {

    arg1 = Convert.ToInt32(dataRow.ItemArray[0]);

    arg2 = dataRow.ItemArray[1].ToString();

    }

    }

     

     

     

    Now, you can use this class as a container for DataRow's data to be passed between application

     

    Tuesday, November 20, 2007 1:45 PM
  • I think something along the lines of what VMazur was talking about is what you're looking for. bola shokry's response isn't very scalabe and it will take lots of time to implement.


    You can use this to serialize the data of that datarow.

    Code Block

            public byte[] SerializeDataRow(System.Data.DataRow row)
            {
                if(row == default(System.Data.DataRow))
                    return default(byte[]);

                System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(object[]));
                serializer.Serialize(memStream, row.ItemArray);

                return memStream.ToArray();
            }

    This is how you code deserialize the data.


    Code Block
            public void DeserializeDataRow(System.Data.DataTable table, byte[] rowBytes)
            {
                if (table == default(System.Data.DataTable) || rowBytes == default(byte[]))
                    return;

                System.IO.MemoryStream memStream = new System.IO.MemoryStream(rowBytes);
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(object[]));
                table.Rows.Add((object[])serializer.Deserialize(memStream));
            }

    Tuesday, November 20, 2007 11:39 PM
  •  

    Hi,

    Many thanks for all your help !!!

     

    This code works fine if all the columns of the datarow and filled.. I mean to say is that if we have dbnull value in any of the column then im getting this exception

    {"The type System.DBNull was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."}

     

    One work around would be to populate data in all the columns.. But is there any proper way of handling this

     

    Awaiting for your reply

    Friday, December 14, 2007 9:38 AM
  •  

    use DataColumn.AllowNull or something to false/true 

    check the methods to not allow Null so they cannot submit unless its filled. I just came across this, for memory stream help. Figured, bored so writing this reply.

    Friday, August 08, 2008 10:29 PM
  • This breaks on serializer.Serialize(memStream, row.ItemArray) when there are null fields.  I have been advised to use XmlInclude and/or SoapInclude attribute but it breaks before I make any web call.  Am I forced to put something in all null fields (objects as it were) ?

    Friday, October 10, 2008 10:54 PM