Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a c# form which stores staff id, name, gender and image (stores only the path to the image not the image)into sql 2008. When am searching for records to be displayed on the form, the records with images get displayed successfully but those without image display this error message “The path is not of a legal form”
I stepped through the code and found that the problem is from there

C#
this.imgStaff.Image = Image.FromFile(person.myPicture.ToString());


what should I do?
Please help me out
Posted
Updated 25-May-12 9:38am
v2

You need to check the Picture and see if it has a value.
C#
if (!string.IsNullOrWhitespace(person.myPicture.ToString()))
   {
   imgStaff.Image = Image.FromFile(person.myPicture.ToString());
   }

If it is already a string (as you say) then you don't need the ToString on either line.
 
Share this answer
 
Comments
Maciej Los 25-May-12 10:42am    
Good answer, my 5!
CodeHawkz 25-May-12 11:43am    
ERrrmmm... Hello??? Does anyone happen to notice that the Image.FromFile method requires the FILE PATH to be passed as the parameter, not the contents. (ref: http://msdn.microsoft.com/en-us/library/e6ytk052)

Secondly, a PICTURE is a BLOB object (i.e. a binary file), not a string file.

Please, correct me if am wrong :) But I seriously think you guys are mistaken.
OriginalGriff 25-May-12 11:52am    
Um.
"and image (stores only the path to the image not the image)"
We might be right...
mikeoabban 25-May-12 14:46pm    
it worked but the error message is still displaying first before the search result is displayed. how do i get raid of this error message?
OriginalGriff 25-May-12 15:36pm    
From the info I have in front of me (one line of code), I have no idea! What Search result? What message?
Check if the person.myPicture has a value before loading it.
 
Share this answer
 

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