Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ch.PoojaBarcode =System.Text.Encoding.Unicode.GetString(BarcodeImage(clsvaboj.Barcode))
here barcode image find but when i insert into table
then i got error
nvarchar is incompatible with image
and in database poojabarcode(columnname)alreay image type.

What I have tried:

nvarchar is incompatible with image
Posted
Updated 9-Aug-16 19:12pm
Comments
Garth J Lancaster 10-Aug-16 1:15am    
if you want to save an image to a NVarChar Column I'm thinking you'd have to convert the image bytes to base64 (characters) ... its not the best option - VarBinary or Binary are better options for a column type - have a look at this https://msdn.microsoft.com/en-us/library/ms187752.aspx

I'd also have serious questions about the Database design and as to wether you need to store images in the database vs in the file system etc and if you really need to store images in the db, optimising the table for image storage

1 solution

The problem is that what you are trying to insert into the database IMAGE column is a string - which is NVARCHAR - and the column requires VARBINARY data, or byte array.
The difference is that a string is made of char values, which are 16 bit values which do not necessarily allow for all the possible values. On the other hand byte data is 8 bit and allows the full range of values in those eight bits: 0 to 255 inclusive.
What you need to do is convert your image to byte data instead of string, and I can't tell you exactly how to do that as I have no idea what your BarcodeImage method returns.
This may help, however: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it shows how to convert an Image to an array of bytes and pass it to a DB.
 
Share this answer
 
Comments
rashmi singhai 10-Aug-16 21:01pm    
i used for BarcodeImage method
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
#region Generate Barcode
private byte[] BarcodeImage(string barcodenum)
{
System.Windows.Forms.PictureBox pic = new System.Windows.Forms.PictureBox();

var type = BarcodeLib.TYPE.UNSPECIFIED;
type = BarcodeLib.TYPE.CODE128;
if (type != BarcodeLib.TYPE.UNSPECIFIED)
{

pic.Image = b.Encode(type, barcodenum, System.Drawing.Color.Black, System.Drawing.Color.White, 250, 40);
}
MemoryStream ms = new MemoryStream();
pic.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] data = ms.GetBuffer();

return data.ToArray();
}
#endregion
OriginalGriff 11-Aug-16 4:02am    
So (ignoring that you are converting a byte array to a byte array) it returns the data you want to save: so why are you then converting that to a string to save it into a binary column? It's already binary data in the right format to directly pass to your DB via a parameter.

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