Hi

my question is about saving array consisting of elements of type UIElement or Button. How can I store them in database and read them from db? Must I conert array to any other type(conversion from UIElement to Byte raises errors). What type column od db should have to store this array?

regards

Choose Image/varbinary/blob/ole column (field) datatype.
Write byte array,

byte []b=.....
        System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();
        System.Data.OleDb.OleDbCommand cmd=new System.Data.OleDb.OleDbCommand("insert into tableName values (col1)", cn);
         
         cmd.Parameters.Add("col1",System.Data.OleDb.OleDbType.Binary,b.Length,"col1");
         cmd.Parameters[0].Value= b;

         cn.Open();
         cmd.ExecuteNonQuery();
         cn.Close();

Read,

...
cmd=new System.Data.OleDb.OleDbCommand("select * from tableName",cn);
         System.Data.OleDb.OleDbDataReader dr;
         dr=cmd.ExecuteReader();
         while(dr.Read())
         {
             byte []t=(byte [])dr[0];
             ....
         }
         dr.Close();

thanks

but the problem is in converting array elements from UIElement to byte. .. It raises error

Type UIElement must be serializable. Use binary serialization.

could you explain me how can I do that?

[Serializable]
public class Test
{
    
}
class MainA
{
   static void Main()
    {
        Test a = new Test();
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        bs.Serialize(ms, a);

        byte[] b = ms.ToArray();
        Console.WriteLine(b.Length);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.