Description: An
unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Illegal characters in path.
Source Error:
Line 150:
Line 151: if(!oldFileName.IsEmpty() && oldFileName!=newFileName){
Line 152: if(File.Exists(Server.MapPath("~/Images/"+ oldFileName))){ Line 153: File.Delete(Server.MapPath("~/Images/"+ oldFileName));
Line 154: }
@{//Checking for user log-inif(!WebSecurity.IsAuthenticated){Response.Redirect("~/Account/Login1");}//..........Layout="~/_Layout.cshtml";Page.Title="Edit a Record";//Image.....WebImagephoto=null;StringnewFileName="";varimagePath="";varimageThumbPath="";varoldFileName="";vardb1=Database.Open("Alumni");varSQL="Select ImageName FROM Info WHERE UserId = @0";oldFileName=db1.QueryValue(SQL,WebSecurity.CurrentUserId);varfname="";varlname="";varinstitu="";varCourse="";varadmission="";varpassOut="";varId="";varcuLoc="";varcuOrg="";varphone="";varmobile="";vardate="";varmonth="";varyear="";varbirthdate="";if(!IsPost){if(!Request.QueryString["ID"].IsEmpty()&&Request.QueryString["ID"].AsInt()>0){Id=Request.QueryString["ID"];vardb=Database.Open("Alumni");vardbCommand="SELECT * FROM Info WHERE UserId = @0";varrow=db.QuerySingle(dbCommand,Id);if(row!=null){fname=row.FirstName;lname=row.LastName;institu=row.Institution;Course=row.Course;admission=row.AdmissionYear.ToString();passOut=row.PassOutYear.ToString();cuLoc=row.CurrentLocation;cuOrg=row.CurrentOrg;phone=row.Phone.ToString();mobile=row.Mobile.ToString();birthdate=row.DateOfBirth.ToString();date=row.DateOfBirth.Date.ToString();month=row.DateOfBirth.Month.ToString();year=row.DateOfBirth.Year.ToString();newFileName=row.ImageName;}else{ModelState.AddFormError("No Student was selected.");}}else{ModelState.AddFormError("No Student was selected.");}}if(IsPost){Validation.RequireField("FirstName","You must enter a FirstName");Validation.RequireField("LastName","You must enter a LastName");Validation.RequireField("Institution","Institution is required");Validation.RequireField("Course","You haven't entered a Course");Validation.RequireField("AdmissionYear","No Admission was submitted!");Validation.RequireField("PassOutYear","Pass-Out-Year is required");Validation.RequireField("CurrentLoc","Current Location is required");Validation.RequireField("CurrentOrg","Current Organization is required");Validation.RequireField("Phone","Phone no. is required");Validation.RequireField("Mobile","Mobile no. is required");fname=Request.Form["FirstName"];lname=Request.Form["LastName"];institu=Request.Form["Institution"];Course=Request.Form["Course"];admission=Request.Form["AdmissionYear"];passOut=Request.Form["PassOutYear"];cuLoc=Request.Form["CurrentLoc"];cuOrg=Request.Form["CurrentOrg"];phone=Request.Form["Phone"];mobile=Request.Form["Mobile"];Id=Request.QueryString["ID"];date=Request.Form["Date"];month=Request.Form["Month"];year=Request.Form["Year"];birthdate=String.Concat(date,"/",month,"/",year).ToString();//Image.......photo=WebImage.GetImageFromRequest();if(photo!=null){newFileName=Guid.NewGuid().ToString()+"_"+Path.GetFileName(photo.FileName).ToString();imagePath=@"Images\"+newFileName;photo.Save(@"~\"+imagePath);imageThumbPath=@"Images\Thumbs\"+newFileName;photo.Resize(width:60,height:60,preserveAspectRatio:true,preventEnlarge:true);photo.Save(@"~\"+imageThumbPath);}vardb=Database.Open("Alumni");if(newFileName==""){//Show uploaded image before updating.varimagename="Select ImageName FROM Info Where UserId=@0";varrow=db.QueryValue(imagename,Id);newFileName=row;}if(Validation.IsValid()){varupdateCommand="UPDATE Info SET FirstName=@0,LastName=@1, Institution=@2, Course=@3,AdmissionYear=@4,PassOutYear=@5,DateOfBirth=@6,CurrentLocation=@7,CurrentOrg=@8,Phone=@9,Mobile=@10,ImageName=@11 WHERE UserId=@12";db.Execute(updateCommand,fname,lname,institu,Course,admission,passOut,birthdate,cuLoc,cuOrg,phone,mobile,newFileName,Id);// Response.Redirect("~/Members/Display.cshtml");}//Delete Imageif(!oldFileName.IsEmpty()&&oldFileName!=newFileName){if(File.Exists(Server.MapPath("~/Images/"+oldFileName))){File.Delete(Server.MapPath("~/Images/"+oldFileName));}}}@oldFileName<br>@newFileName}<h2>Edit Record</h2>@Html.ValidationSummary()<formaction=""method="post"enctype="multipart/form-data"><fieldset><legend> Fill in Details</legend>
FirstName: <inputtype="text"name="FirstName"value="@fname"/> LastName: <inputtype="text"name="LastName"value="@lname"/></br></br>
Institution: <selectname="Institution"><optionvalue="Institute of Technology">SMES's Institute of technology</option><optionvalue="SMES's Institute of Pharmacy">SMES's Institute of Pharmacy</option><optionvalue="Sanghavi College of Engineering">Sanghavi College of Engineering</option></select></br></br>
Course: <selectname="Course"><optionvalue="Computer Engg">CO</option><optionvalue="Civil Engg">CE</option><optionvalue="Mechanical Engg">ME</option><optionvalue="Electronics & Telecommunications">E&TC</option></select></br></br>
Year of Admission: <inputtype="text"name="AdmissionYear"value="@admission"/> PassYear: <inputtype="text"name="PassOutYear"value="@passOut"/></br></br>
Date: <inputtype="number"name="Date"value="@date"/> Month:<selectname="Month"><optionvalue="January">January</option><optionvalue="February">February</option><optionvalue="March">March</option><optionvalue="April">April</option><optionvalue="May">May</option><optionvalue="June">June</option><optionvalue="July">July</option><optionvalue="August">August</option><optionvalue="September">September</option><optionvalue="October">October</option><optionvalue="November">November</option><optionvalue="December">December</option></select> Year<inputtype="number"name="Year"/></br></br>
Current Location: <inputtype="text"name="CurrentLoc"value="@cuLoc"/></br></br>
Current Organization:<inputtype="text"name="CurrentOrg"value="@cuOrg"/></br></br>
Phone no: <inputtype="text"name="Phone"value="@phone"/></br></br>
Mobile:<inputtype="text"name="Mobile"value="@mobile"/></br></br><labelfor="Image">Image</label><br><div><imgsrc="@Href("~/Images/"+@newFileName)"alt="image"width="70"height="70"/></div><inputtype="file"name="Image"/><div><inputtype="submit"value="submit"/></div></fieldset></form><p><ahref="~/Members/Edit.cshtml">Return Records listing</a></p>
Problem solved! oldFileName was empty! Database was empty! It was checking for old filename which didnt return any value, so File.Exists searched for NULL!
heres a code ! Check it if it is correct and my above answer makes sense!
@{//Checking for user log-inif(!WebSecurity.IsAuthenticated){Response.Redirect("~/Account/Login1");}//..........Layout="~/_Layout.cshtml";Page.Title="Edit a Record";//Image.....WebImagephoto=null;StringnewFileName="";varimagePath="";varimageThumbPath="";varoldFileName="";varfname="";varlname="";varinstitu="";varCourse="";varadmission="";varpassOut="";varId="";varcuLoc="";varcuOrg="";varphone="";varmobile="";vardate="";varmonth="";varyear="";varbirthdate="";if(!IsPost){if(!Request.QueryString["ID"].IsEmpty()&&Request.QueryString["ID"].AsInt()>0){Id=Request.QueryString["ID"];vardb=Database.Open("Alumni");vardbCommand="SELECT * FROM Info WHERE UserId = @0";varrow=db.QuerySingle(dbCommand,Id);if(row!=null){fname=row.FirstName;lname=row.LastName;institu=row.Institution;Course=row.Course;admission=row.AdmissionYear.ToString();passOut=row.PassOutYear.ToString();cuLoc=row.CurrentLocation;cuOrg=row.CurrentOrg;phone=row.Phone.ToString();mobile=row.Mobile.ToString();birthdate=row.DateOfBirth.ToString();date=row.DateOfBirth.Date.ToString();month=row.DateOfBirth.Month.ToString();year=row.DateOfBirth.Year.ToString();newFileName=row.ImageName;}else{ModelState.AddFormError("No Student was selected.");}}else{ModelState.AddFormError("No Student was selected.");}}if(IsPost){Validation.RequireField("FirstName","You must enter a FirstName");Validation.RequireField("LastName","You must enter a LastName");Validation.RequireField("Institution","Institution is required");Validation.RequireField("Course","You haven't entered a Course");Validation.RequireField("AdmissionYear","No Admission was submitted!");Validation.RequireField("PassOutYear","Pass-Out-Year is required");Validation.RequireField("CurrentLoc","Current Location is required");Validation.RequireField("CurrentOrg","Current Organization is required");Validation.RequireField("Phone","Phone no. is required");Validation.RequireField("Mobile","Mobile no. is required");fname=Request.Form["FirstName"];lname=Request.Form["LastName"];institu=Request.Form["Institution"];Course=Request.Form["Course"];admission=Request.Form["AdmissionYear"];passOut=Request.Form["PassOutYear"];cuLoc=Request.Form["CurrentLoc"];cuOrg=Request.Form["CurrentOrg"];phone=Request.Form["Phone"];mobile=Request.Form["Mobile"];Id=Request.QueryString["ID"];date=Request.Form["Date"];month=Request.Form["Month"];year=Request.Form["Year"];birthdate=String.Concat(date,"/",month,"/",year).ToString();//Image.......photo=WebImage.GetImageFromRequest();if(photo!=null){newFileName=Guid.NewGuid().ToString()+"_"+Path.GetFileName(photo.FileName).ToString();imagePath=@"Images\"+newFileName;photo.Save(@"~\"+imagePath);imageThumbPath=@"Images\Thumbs\"+newFileName;photo.Resize(width:60,height:60,preserveAspectRatio:true,preventEnlarge:true);photo.Save(@"~\"+imageThumbPath);}vardb=Database.Open("Alumni");if(newFileName==""){//Show uploaded image before updating.varimagename="Select ImageName FROM Info Where UserId=@0";varrow=db.QueryValue(imagename,Id);newFileName=row;}if(Validation.IsValid()){varSQLselect="Select ImageName FROM Info WHERE UserId=@0";//Select oldFilename to delete old image.varcuname=db.QueryValue(SQLselect,Id);oldFileName=cuname;varupdateCommand="UPDATE Info SET FirstName=@0,LastName=@1, Institution=@2, Course=@3,AdmissionYear=@4,PassOutYear=@5,DateOfBirth=@6,CurrentLocation=@7,CurrentOrg=@8,Phone=@9,Mobile=@10,ImageName=@11 WHERE UserId=@12";db.Execute(updateCommand,fname,lname,institu,Course,admission,passOut,birthdate,cuLoc,cuOrg,phone,mobile,newFileName,Id);//Image Deleteif(!oldFileName.IsEmpty()&&oldFileName!=newFileName){if(File.Exists(Server.MapPath("~/Images/"+oldFileName))){File.Delete(Server.MapPath("~/Images/"+oldFileName));}}Response.Redirect("~/Members/Display.cshtml");}}}
Marked as answer by red77 on Dec 30, 2012 07:07 AM
Red77
Member
1 Points
26 Posts
Illegal characters in path.
Dec 29, 2012 09:47 PM|LINK
Illegal characters in path.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Illegal characters in path.
Source Error:
Line 150: Line 151: if(!oldFileName.IsEmpty() && oldFileName!=newFileName){ Line 152: if(File.Exists(Server.MapPath("~/Images/"+ oldFileName))){ Line 153: File.Delete(Server.MapPath("~/Images/"+ oldFileName)); Line 154: }Line: 152
Stack Trace:
wavemaster
Participant
1279 Points
1125 Posts
Re: Illegal characters in path.
Dec 29, 2012 10:43 PM|LINK
Open up visual studio and then open up this file.
Put a breakpoint in and run the code.
Hoover over the various variables to determine what that illegal character is.
oned_gk
All-Star
31017 Points
6352 Posts
Re: Illegal characters in path.
Dec 29, 2012 11:34 PM|LINK
Red77
Member
1 Points
26 Posts
Re: Illegal characters in path.
Dec 30, 2012 06:22 AM|LINK
So is my above code and logic ok?
dotnetramesh
Member
74 Points
37 Posts
Re: Illegal characters in path.
Dec 30, 2012 06:40 AM|LINK
remove the tilled(~) charector.. then check
Red77
Member
1 Points
26 Posts
Re: Illegal characters in path.
Dec 30, 2012 07:06 AM|LINK
Problem solved! oldFileName was empty! Database was empty! It was checking for old filename which didnt return any value, so File.Exists searched for NULL!
heres a code ! Check it if it is correct and my above answer makes sense!