473,414 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

byte[] from datarow raise exception

Hello,

I want to read image (string) from XML file to picture box
Here my code:
// Listbox doube click event
private void lbImages_DoubleClick(object sender, System.EventArgs e)
{
string currentItem;

currentItem = lbImages.Text;
if (currentItem == null) return;
string expr = "name = '" + currentItem + "'";
DataRow dr = m_DataSet.Tables["data"].Select(expr)[0];

byte[] img = (byte[])(dr["value"]); ///// ---> Error here

System.IO.MemoryStream ms = new System.IO.MemoryStream();
int offset = 78;
ms.Write(img, offset, img.Length - offset);
Bitmap bm = new Bitmap(ms);
ms.Close();
frmImageViewer viewer = new frmImageViewer(bm);
viewer.Show();
}

Compiled find but when runtime, exception raised ""Specified cast is not valid".
Please help me

Thanks,

sonvu
Nov 16 '05 #1
7 8440
sonvu <an*******@microsoft.com> wrote:
I want to read image (string) from XML file to picture box
Here my code:
// Listbox doube click event
private void lbImages_DoubleClick(object sender, System.EventArgs e)
{
string currentItem;

currentItem = lbImages.Text;
if (currentItem == null) return;
string expr = "name = '" + currentItem + "'";
DataRow dr = m_DataSet.Tables["data"].Select(expr)[0];

byte[] img = (byte[])(dr["value"]); ///// ---> Error here

System.IO.MemoryStream ms = new System.IO.MemoryStream();
int offset = 78;
ms.Write(img, offset, img.Length - offset);
Bitmap bm = new Bitmap(ms);
ms.Close();
frmImageViewer viewer = new frmImageViewer(bm);
viewer.Show();
}

Compiled find but when runtime, exception raised ""Specified cast is not valid".
Please help me


And what is the type of dr["value"] at runtime? That's the crucial
thing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jon Skeet [C# MVP] wrote:
And what is the type of dr["value"] at runtime? That's the crucial
thing.


Hello Jon,
(Sorry my bad English)
Yeah, I have a resX file and I want to get it's images and display in a picturebox
i.e
<data name="pictureBox1.Image" type="System.Drawing.Bitmap, System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPER ET
FhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4e
Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh 7/wAARCAEsAZADASIAAhEBAxEB/8QA
.............
.............
.............
</value>

The dr["value"].ToString() = content of value tag

Thanks,

sonvu
Nov 16 '05 #3
sonvu <an*******@microsoft.com> wrote:
Jon Skeet [C# MVP] wrote:
And what is the type of dr["value"] at runtime? That's the crucial
thing.


(Sorry my bad English)
Yeah, I have a resX file and I want to get it's images and display in a picturebox
i.e
<data name="pictureBox1.Image" type="System.Drawing.Bitmap, System.Drawing,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPER ET
FhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4e
Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh 7/wAARCAEsAZADASIAAhEBAxEB/8QA
.............
.............
.............
</value>

The dr["value"].ToString() = content of value tag


So what's the type of dr["value"] itself? What happens if you look at
dr["value"].GetType() in the debugger?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon Skeet [C# MVP] wrote:

So what's the type of dr["value"] itself? What happens if you look at
dr["value"].GetType() in the debugger?


Ah, my bad. I missunderstood your question, dr["value"] is "System.String" type.

Thanks for relply,

sonvu
Nov 16 '05 #5
sonvu <an*******@microsoft.com> wrote:
Jon Skeet [C# MVP] wrote:

So what's the type of dr["value"] itself? What happens if you look at
dr["value"].GetType() in the debugger?


Ah, my bad. I missunderstood your question, dr["value"] is
"System.String" type.


Right. In that case, you need to work out how to convert it into a byte
array. It looks like it's base64-encoded data, so use
Convert.FromBase64String.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Hello Jon,
Got it, heh he.
My code now as below:
byte[] img = (byte[])(Convert.FromBase64String(dr["value"].ToString()));

Thanks again,

sonvu

Nov 16 '05 #7
sonvu <an*******@microsoft.com> wrote:
Got it, heh he.
My code now as below:
byte[] img = (byte[])(Convert.FromBase64String(dr["value"].ToString()));


You don't need to cast the result of Convert.FromBase64String as it's
declared to return a byte array. However, I would cast the result of
dr["value"] to string rather than calling ToString(), as if the value
isn't a string then it's unlikely it's correct to start with.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nicolas Fleury | last post by:
Hi, I've made a small utility to re-raise an exception with the same stack as before with additional information in it. Since I want to keep the same exception type and that some types have very...
1
by: Wasim Akram | last post by:
Hi, I have a field "Month" in my SQL server table. The type of this field is "tinyint". Now what I am doing in the code is using DataRow to read this field in a 'int' variable. int month...
8
by: Brad Wood | last post by:
When I do this: foreach( Button btn in myForm.Controls ) An exception is raised when no buttons exist. I would simply expect execution to continue after the foreach loop (just as would be the...
2
by: GS | last post by:
I have noticed that ASP.NET 2.0 writes unhandled exceptions automatically to event log. How do I that on purpouse in a code? Say I have hit error condition and would like to abort current thread and...
1
by: Bryan | last post by:
hi, i would like to save an exception and reraise it at a later time. something similar to this: exception = None def foo():     try:
2
by: crybaby | last post by:
Why you specify type and name of the exception in your custom exceptions, but not in built in exceptions except IOError: print "no file by the name ccy_rates*.txt" except MyError, e: print...
4
by: Alex G | last post by:
Does anyone know how I would go about conditionally raising an exception in a decorator (or any returned function for that matter)? For example: def decorator(arg): def raise_exception(fn):...
3
by: Robert Rawlins | last post by:
Hi Mk, Yeah it's got me a little bemused to be honest, I've tried playing around with configuration options this morning and not been able to achieve anything that works properly. I'll keep...
14
by: Rafe | last post by:
Hi, I've encountered a problem which is making debugging less obvious than it should be. The @property decorator doesn't always raise exceptions. It seems like it is bound to the class but...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.