469,963 Members | 2,086 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

Page fails if there is no Image uploaded


I have a webform that uload images as well. The image is uploaded well but the problem arises when the user does not upload any image. I want the form to save nothing for the image_name in the database if there is no image loaded but it does not get to the art of saving to the database, please see my code below and the error I am getting:

Expand|Select|Wrap|Line Numbers
  1. if( filMyFile.PostedFile != null )
  2.  {
  4.      string strFilename;  
  5.      strFilename = filMyFile.PostedFile.FileName;    
  6.      strFilename = System.IO.Path.GetFileName(strFilename);
  7.      filMyFile.PostedFile.SaveAs(@"C:\Advert_images\"+strFilename);
  8.  }
  9. else
  10. {
  12. // No file
  14. }
This is the error I am getting when there is no image uploaded:
Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path "C:\Advert_images\".

Any help will be highly appreaciated.
May 7 '09 #1
8 1358
344 100+
The exception make me think that your system have no folder with the name Advert_images in partition C.
But what line arise this exception.
May 11 '09 #2
Hi Bassem, thanks. It fails on the line 'filMyFile.PostedFile.SaveAs(@"C:\Advert_images\"+ strFilename', I would have thought it does not have to go inside the if statement if filMyFile.PostedFile is null, it would simply go to the ELSE part and do nothing?
May 12 '09 #3
344 100+
Sorry for late, my keyboard is damaged and I had to buy a new one.
filMyFile.PostedFile will not equal null at any way. So check like this:
Expand|Select|Wrap|Line Numbers
  1.         if (filMyFile.PostedFile.ContentLength != 0)
  2.         {
  3.             string strFilename;
  4.             strFilename = filMyFile.PostedFile.FileName;
  5.             strFilename = System.IO.Path.GetFileName(strFilename);
  6.             filMyFile.PostedFile.SaveAs(@"C:\Advert_images\" + strFilename);
  7.         }
  8.         else
  9.         {
  11.             // No file
  13.         }
Now the exception arise because the folder Advert_images doesn't exist. Be sure you already created it.

May 13 '09 #4
Thanks, it works like a charm when I use if (filMyFile.PostedFile.ContentLength != 0) . Sorry but I'm new to .Net programming so I need help again. I am trying to display a computer name in a texbox but I am failing, please look at my code below:

string macname = Environment.MachineName;
txtECN.Text = macname[0].ToString();
May 13 '09 #5
How do I concatenate two textbox values into one table column, can anyone please help?
May 14 '09 #6
Curtis Rutland
3,256 Expert 2GB
What kind of table are you talking about?

String concatenation is simple enough:
Expand|Select|Wrap|Line Numbers
  1. string s = textbox1.Text + textbox2.Text;
Also, please don't keep posting new questions in your old thread. One topic, one thread. New topic, start a new thread.

May 14 '09 #7
344 100+
Environment.MachineName returns a string, but in the next line you get only the first character only macname[0].

It should be like that :
Expand|Select|Wrap|Line Numbers
  1. string macname =   Environment.MachineName;
  2. txtECN.Text = macname;
May 14 '09 #8
Thanks for all your help
May 15 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Gary \(Girish\) | last post: by
11 posts views Thread by bissatch | last post: by
2 posts views Thread by Paul Gorman | last post: by
6 posts views Thread by Dave Keen | last post: by
2 posts views Thread by luke noob | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.