How do I deal with image datatype in linq.
-
Tuesday, January 01, 2008 4:31 PM
How do I deal with image datatype in linq. I am trying to add and retrieve images from a DB table and I am getting this error with a straight conversion of my old code. The class that mapps to the image table is expectins a binaary type instead of a byte array
Value of type 'System.Data.Linq.Binary' cannot be converted to '1-dimensional array of Byte
If Not (image Is Nothing AndAlso image.imageId > 0) Thencontext.Response.Clear()
context.Response.BufferOutput =
Falsecontext.Response.OutputStream.Write(
CType(image.imageData, Byte()), 0, CInt(image.imageSize))context.Response.End()
End If
All Replies
-
Tuesday, January 01, 2008 7:13 PM
You can change the mapping to have the property be 'byte[]' instead of 'Binary'. There are advantages to having 'Binary' though. You can also get a 'byte[]' from a 'Binary' using 'binary.ToArray()'.
-
Tuesday, January 01, 2008 8:02 PM
Hi,
In my first attempt (using Northwind.Employee.Photo column) I kept getting the following exception:
System.ArgumentException was unhandled
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Bitmap..ctor(Stream stream)
at LinqPOC.Program.Q1() in D:\Users\Ben Hall\Documents\Visual Studio 2008\Projects\LinqPOC\LinqPOC\Class1.cs:line 37
at LinqPOC.Program.Main(String[] args) in D:\Users\Ben Hall\Documents\Visual Studio 2008\Projects\LinqPOC\LinqPOC\Class1.cs:line 19
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:The code:
Code BlockMemoryStream ms = new MemoryStream(item.Photo.ToArray());
Bitmap bmp = new Bitmap(ms);
bmp.Save(Path.GetTempFileName());
Turns out Northwind is a bit strange and includes the header - http://www.akadia.com/services/dotnet_load_blob.html
Changed the code to this:
Code BlockByte[] img = item.Photo.ToArray();
MemoryStream ms = new MemoryStream();
//http://www.akadia.com/services/dotnet_load_blob.html
int offset = 78;ms.Write(img, offset, img.Length - offset);
Bitmap bmp = new Bitmap(ms); string path = Path.GetTempFileName(); Console.WriteLine(path);bmp.Save(path);
This just removes the header, this then works fine.
Hope this helps you.
Ben
-
Thursday, January 24, 2008 7:44 AMHullo Matt Warren
am using Asp.net 3.5, Visual Studio 2008. I upload some files(image,text file) to image datatype column as binary format using LINQ , now i want to retrieve those files, i have write the following code
ArrayList alFileContent = new ArrayList();
ArrayList alFileFormat = new ArrayList();
ArrayList alFileSize = new ArrayList();
string datatype1 = "System.String"; string datatype2 = "System.Byte[]";
string datatype3 = "System.String"; string datatype4 = "System.Int64";
DataContext dc = new DataContext(Convert.ToString(Session["LINQConnectionString"]));
DataTable dt = new DataTable();
dc.Connection.Open();
DataColumn dc_filename = new DataColumn(Convert.ToString(Session["FileNameColumnName"]));
dc_filename.DataType = System.Type.GetType(datatype1);
dt.Columns.Add(dc_filename);
DataColumn dc_filecontent = new DataColumn(Convert.ToString(Session["FileContentColumnName"]));
dc_filecontent.DataType = System.Type.GetType(datatype2);
dt.Columns.Add(dc_filecontent);
DataColumn dc_fileformat = new DataColumn(Convert.ToString(Session["FormatColumnName"]));
dc_fileformat.DataType = System.Type.GetType(datatype3);
dt.Columns.Add(dc_fileformat);
DataColumn dc_filesize = new DataColumn(Convert.ToString(Session["FileSizeColumnName"]));
dc_filesize.DataType = System.Type.GetType(datatype4);
dt.Columns.Add(dc_filesize);
var SelectQueryForFileName = dc.ExecuteQuery<string>(Convert.ToString(Session["PassQueryToImageGrabForFileName"]));
var SelectQueryForFileFormat = dc.ExecuteQuery<string>(Convert.ToString(Session["PassQueryToImageGrabForFileFormat"]));// BELOW LINE IS MY PROBLEM
var SelectQueryForFileContent = dc.ExecuteQuery<byte>("Select file_content from FILE_MASTER");when the above line is reached then i got the error as specified cast is not valid . i understand this occur due to datatype conversion, but how can i read the binary data from table. If i use byte[] then it return an error message as
The type 'System.Byte[]' must declare a default (parameterless) constructor in order to be constructed during mapping
Please anybody answer me . Thank you. -
Friday, February 01, 2008 8:32 PM
-
Saturday, February 02, 2008 6:39 AMI found it looks like a microsoft bug, i read this in the following folder
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=697469&SiteID=1

