Posts

Showing posts from February, 2016

storing byte array as column value in datatable using c#

This post covers following objective 1. How to declare and define datatable 2. Add columns at runtime 3. Add column to store byte array (image/file etc) //YOUR PHOTOGRAPH HERE MemoryStream ms = new MemoryStream(); argPhotograph.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); //DECLARING DATATABLE DataTable dt = new DataTable ();   dt . Columns . Add ( "ID" ); dt . Columns . Add ( "Name" ); dt . Columns . Add ( "CreatedOn" , typeof ( DateTime )); dt . Columns . Add ( "Photograph" , typeof ( byte []));   //METHOD 1: ADDING DATA DataRow row = null; row = dt.NewRow(); row[ "ID"] = 1; row[ "Name"] = "Usman"; row[ "CreatedOn"] = DateTime.Now; row[ "Photograph"] = argPhotograph.ToArray(); dt.Rows.Add(row);  //METHOD 2: ADDING DATA dt.Row.Add( 1, "Usman", DateTime.Now, argPhotograph.ToArray());