I am a beginner to ASP.Net Web Pages using razor. Trying to build a site for the first time.... I need some help regarding String format was incorrect while updating/inserting Image details:
fileTitle = Path.GetFileNameWithoutExtension(photo.FileName).Trim() ;
fileExtension = Path.GetExtension(photo.FileName).Trim();
-------------------------------------------------------------------------------
12 : - Input string was not in a correct format.
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.FormatException: 12 : - Input string was not in a correct format.
Source Error:
Line 128: var db = Database.Open("Alumni");
Line 129: var updateCommand = "UPDATE Info SET FirstName=@0,LastName=@1, Institution=@2, Course=@3,AdmissionYear=@4,PassOutYear=@5,DateOfBirth=@6,CurrentLocation=@7,CurrentOrg=@8,Phone=@9,Mobile=@9,ImageName=@10,Extension=@11 WHERE UserId=@12";Line 130: db.Execute(updateCommand, fname, lname,institu, Course, admission,passOut,birthdate,cuLoc,cuOrg, phone, mobile,fileTitle,Id); Line 131: Response.Redirect("~/Members/Display.cshtml");
Line 132: } |
Source File: c:\Users\XBlaZE\Desktop\Project-X\Test1\Members\Update.cshtml Line: 130
Stack Trace:
[FormatException: 12 : - Input string was not in a correct format.] System.Data.SqlServerCe.SqlCeCommand.FillParameterDataBindings(Boolean verifyValue) +1183 System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) +602 System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery() +21 WebMatrix.Data.Database.Execute(String commandText, Object[] args) +122 ASP._Page_Members_Update_cshtml.Execute() in c:\Users\XBlaZE\Desktop\Project-X\Test1\Members\Update.cshtml:130 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207 System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68 System.Web.WebPages.WebPage.ExecutePageHierarchy() +156 System.Web.WebPages.StartPage.RunPage() +19 System.Web.WebPages.StartPage.ExecutePageHierarchy() +65 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76 System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +119 |
Following is the overall code for that page...
@{ //Checking for user log-in if (!WebSecurity.IsAuthenticated) { Response.Redirect("~/Account/Login1"); } //.......... Layout = "~/_Layout.cshtml"; Page.Title = "Edit a Record"; //Image..... WebImage photo = null; var newFileName = ""; var imagePath = ""; var imageThumbPath = ""; var fileTitle = ""; var fileExtension = ""; var fname = ""; var lname = ""; var institu = ""; var Course = ""; var admission = ""; var passOut = ""; var Id = ""; var cuLoc = ""; var cuOrg = ""; var phone = ""; var mobile = ""; var date = Request.Form["Date"]; var month = Request.Form["Month"]; var year = Request.Form["Year"]; var birthdate = String.Concat(date, "/", month, "/", year); if(!IsPost){ if(!Request.QueryString["ID"].IsEmpty() && Request.QueryString["ID"].AsInt() > 0 ) { Id = Request.QueryString["ID"]; var db = Database.Open("Alumni"); var dbCommand = "SELECT * FROM Info WHERE UserId = @0"; var row = 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(); birthdate = row.DateOfBirth.ToString(); cuLoc = row.CurrentLocation; cuOrg = row.CurrentOrg; phone = row.Phone.ToString(); mobile = row.Mobile.ToString(); birthdate = row.DateOfBirth.ToString(); date = birthdate.Substring(0,2); month = birthdate.Substring(2); } 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["CorrentLoc"]; cuOrg = Request.Form["CorrentOrg"]; phone = Request.Form["Phone"]; mobile = Request.Form["Mobile"]; date = Request.Form["Date"]; month = Request.Form["Month"]; year = Request.Form["Year"]; birthdate = String.Concat(date, "/", month, "/", year); //Image....... photo = WebImage.GetImageFromRequest(); if(photo != null){ newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo.FileName); imagePath = @"Images\" + newFileName; photo.Save(@"~\" + imagePath); imageThumbPath = @"Images\Thumbs\" + newFileName; photo.Resize(width: 60, height: 60, preserveAspectRatio: true, preventEnlarge: true); photo.Save(@"~\" + imageThumbPath); } fileTitle = Path.GetFileNameWithoutExtension(photo.FileName).Trim(); fileExtension = Path.GetExtension(photo.FileName).Trim(); if(Validation.IsValid()){ var db = Database.Open("Alumni"); var updateCommand = "UPDATE Info SET FirstName=@0,LastName=@1, Institution=@2, Course=@3,AdmissionYear=@4,PassOutYear=@5,DateOfBirth=@6,CurrentLocation=@7,CurrentOrg=@8,Phone=@9,Mobile=@9,ImageName=@10,Extension=@11 WHERE UserId=@12"; db.Execute(updateCommand, fname, lname,institu, Course, admission,passOut,birthdate,cuLoc,cuOrg, phone, mobile,fileTitle,Id); Response.Redirect("~/Members/Display.cshtml"); } } }<h2>Edit Record</h2> @Html.ValidationSummary()<form action="" method="post" enctype="multipart/form-data"><fieldset><legend> Fill in Details</legend> FirstName: <input type="text" name="FirstName" value="@fname"/> LastName: <input type="text" name="LastName" value="@lname"/></br></br> Institution: <select name="Institution"><option value="Institute of Technology">SMES's Institute of technology</option><option value="SMES's Institute of Pharmacy">SMES's Institute of Pharmacy</option><option value="Sanghavi College of Engineering">Sanghavi College of Engineering</option></select></br></br> Course: <select name="Course"><option value="Computer Engg">CO</option><option value="Civil Engg">CE</option><option value="Mechanical Engg">ME</option><option value="Electronics & Telecommunications">E&TC</option></select> </br> </br> Year of Admission: <input type="text" name="AdmissionYear" value="@admission"/> PassYear: <input type="text" name="PassOutYear" value="@passOut"/> </br></br> Date: <input type="number" name="Date" value="@date"/> Month:<select name="Month" ><option value="January">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option><option value="May">May</option><option value="June">June</option><option value="July">July</option><option value="August">August</option><option value="September">September</option><option value="October">October</option><option value="November">November</option><option value="December">December</option></select> Year<input type="number" name="Year"/> </br> </br> Current Location: <input type="text" name="CurrentLoc" value="@cuLoc"/> </br> </br> Current Organization:<input type="text" name="CurrentOrg" value="@cuOrg"/> </br> </br> Phone no: <input type="text" name="Phone" value="@phone"/> </br> </br> Mobile:<input type="text" name="Mobile" value="@mobile"/> </br> </br><input type="hidden" name="ID" /> </br> </br><label for="Image">Image</label><br><input type="file" name="Image" /><div><input type="submit" value="submit"/></div></fieldset></form><p><a href="~/Members/Edit.cshtml">Return Records listing</a></p>
Any assistence in above code to check whether it is optimized or not efficient is welcomed
I m not a pro nor intermediate.. just modifying the templates from webmatrix...
One more thing, currOrg does not enter any value into database... it shows blank field even though Update/Insert commands work!