Connecting Tech Pros Worldwide Forums | Help | Site Map

Page fails if there is no Image uploaded

Newbie
 
Join Date: Apr 2009
Posts: 17
#1: May 7 '09
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.

Familiar Sight
 
Join Date: Dec 2008
Posts: 202
#2: May 11 '09

re: Page fails if there is no Image uploaded


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.
Newbie
 
Join Date: Apr 2009
Posts: 17
#3: May 12 '09

re: Page fails if there is no Image uploaded


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?
Familiar Sight
 
Join Date: Dec 2008
Posts: 202
#4: May 13 '09

re: Page fails if there is no Image uploaded


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
Newbie
 
Join Date: Apr 2009
Posts: 17
#5: May 13 '09

re: Page fails if there is no Image uploaded


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();
Newbie
 
Join Date: Apr 2009
Posts: 17
#6: May 14 '09

re: Page fails if there is no Image uploaded


How do I concatenate two textbox values into one table column, can anyone please help?
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#7: May 14 '09

re: Page fails if there is no Image uploaded


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
Familiar Sight
 
Join Date: Dec 2008
Posts: 202
#8: May 14 '09

re: Page fails if there is no Image uploaded


Quote:

Originally Posted by zizi2 View Post

string macname = Environment.MachineName;
txtECN.Text = macname[0].ToString();

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.  
Newbie
 
Join Date: Apr 2009
Posts: 17
#9: May 15 '09

re: Page fails if there is no Image uploaded


Thanks for all your help
Reply