Click here to Skip to main content
13,005,991 members (107,099 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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++)

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
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
    // close the binary reader
    // 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!
sharadpanwal 31-Oct-12 5:25am
Still giving same error Type of value has a mismatch with column typeCouldn't store <System.Byte[]> in logoname Column. Expected type is Byte[].
_Amy 31-Oct-12 5:34am
Try my updated answer.. Use typeof(byte[]) directly.
sharadpanwal 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170628.1 | Last Updated 31 Oct 2012
Copyright © CodeProject, 1999-2017
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