By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

Page fails if there is no Image uploaded

P: 17
Hi,

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.  {
  3.  
  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. {
  11.  
  12. // No file
  13.  
  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
Share this Question
Share on Google+
8 Replies


100+
P: 344
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

P: 17
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

100+
P: 344
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.         {
  10.  
  11.             // No file
  12.  
  13.         }
Now the exception arise because the folder Advert_images doesn't exist. Be sure you already created it.

Regards,
Bassem
May 13 '09 #4

P: 17
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

P: 17
How do I concatenate two textbox values into one table column, can anyone please help?
May 14 '09 #6

Curtis Rutland
Expert 2.5K+
P: 3,256
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.

MODERATOR
May 14 '09 #7

100+
P: 344
@zizi2
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;
  3.  
May 14 '09 #8

P: 17
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.