Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET DataTable
hi, i want to add image to datatble but getting an error.below is code i have done.
 
Type of value has a mismatch with column typeCouldn't store <system.byte[]> in logoname Column. Expected type is Byte[].
 
this error i got at this line dt.Rows.Add(imageDataSet.Tables[0].Rows[rowNumber][0].ToString());
 

DataTable dt = new DataTable();
dt.Columns.Add("logoname", System.Type.GetType("System.Byte[]"));
 
 for (int rowNumber = 0; rowNumber < imageDataSet.Tables[0].Rows.Count; rowNumber++)
          {
              DisplayImages(imageDataSet.Tables[0].Rows[rowNumber],"abc.jpg");
              dt.Rows.Add(imageDataSet.Tables[0].Rows[rowNumber][0].ToString());
          }
 

 
private void DisplayImages(DataRow row, string ImagePath)
        {
            FileStream imageStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(imageStream);
           
            row[0] = br.ReadBytes(Convert.ToInt32(br.BaseStream.Length));
        }
Posted 30-Oct-12 23:30pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to create a function which will convert an image to byte array. Try this:
private byte[] GetByteArray(String strFileName)
{
    System.IO.FileStream fs = new System.IO.FileStream(strFileName, System.IO.FileMode.Open);
    // initialise the binary reader from file streamobject
    System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
    // define the byte array of filelength

    byte[] imgbyte = new byte[fs.Length + 1];
    // read the bytes from the binary reader

    imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));
    // add the image in bytearray
    
    br.Close();
    // close the binary reader

    fs.Close();
    // close the file stream
    return imgbyte;
}
Now call the above function and add your image to datatable. Try this:
DataTable dt = new DataTable();
dt.Columns.Add("Image", typeof(byte[]));
dt.Rows[0]["Image"] = GetByteArray(strFilePath);
//this will add a row with the image. Accordingly, you need to loop and add the rows as required.
 
Hope it helps!
     --Amit
  Permalink  
v2
Comments
sharadpanwal at 31-Oct-12 5:25am
   
Still giving same error Type of value has a mismatch with column typeCouldn't store in logoname Column. Expected type is Byte[].
_Amy at 31-Oct-12 5:34am
   
Try my updated answer.. Use typeof(byte[]) directly.
sharadpanwal at 31-Oct-12 5:38am
   
tried updated answer,but Again same error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 31 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100