Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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());


C#
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());
          }



C#
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

1 solution

You need to create a function which will convert an image to byte array. Try this:
C#
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:
C#
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
 
Share this answer
 
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900