Click here to Skip to main content
11,577,258 members (59,313 online)
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 22: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 DamithSL 155
1 Afzaal Ahmad Zeeshan 125
2 OriginalGriff 115
3 Richard MacCutchan 100
4 Abhinav S 90
0 OriginalGriff 820
1 Sergey Alexandrovich Kryukov 631
2 Abhinav S 528
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 31 Oct 2012
Copyright © CodeProject, 1999-2015
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