I am a beginner to ASP.Net. Creating my 1st site with razor syntax. No previous knowledge about ASP. So I m learning about it right now by modifying the template sites. Any modifications in the existing code to optimize it is welcomed.
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: }
@{//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="";//String fileTitle = "";// String fileExtension = "";varfname="";varlname="";varinstitu="";varCourse="";varadmission="";varpassOut="";varId="";varcuLoc="";varcuOrg="";varphone="";varmobile="";vardate=Request.Form["Date"];varmonth=Request.Form["Month"];varyear=Request.Form["Year"];varbirthdate=String.Concat(date,"/",month,"/",year);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();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["CurrentLoc"];cuOrg=Request.Form["CurrentOrg"];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).ToString();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().ToString();if(Validation.IsValid()){vardb=Database.Open("Alumni");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=@9,ImageName=@10 WHERE UserId=@11";db.Execute(updateCommand,fname,lname,institu,Course,admission,passOut,birthdate,cuLoc,cuOrg,phone,mobile,newFileName,Id); Response.Redirect("~/Members/Display.cshtml");}}}<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><inputtype="hidden"name="ID"/></br></br><labelfor="Image">Image</label><br><inputtype="file"name="Image"/><div><inputtype="submit"value="submit"/></div></fieldset></form><p><ahref="~/Members/Edit.cshtml">Return Records listing</a></p>
This error is because the variable you are using has some null value while the database table you are accessing does not allows nulls
Somehow it can also be because sometime user input data as a string while the data in database is datetime. Your user may wants to input 12 while the database needs something else.
You need to execute data. And the 12 you are entering is somewhat an interger. Try covering the variable and converting to string() like
var int_to_string = "12".ToString();
This would do it.
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
Well, the real problem is the Image name to be inserted into database. This is one problem.
Second problem is of CurrentOrganization. My Database has this column "CurrentOrg" which does allow nulls!
Problem 1: When i insert image fileName or fileExtension, it gives error String not in proper format. Both the columns for these are allowing null values. I m using Guid which is converted to string... prefixed to filename of image. Whats the problem?
Problem 2: CurrentOrg in database is Null while all records are being inserted/Updated. If there is any problem with CurrentOrg about String format, it should give me this error that i m getting now(After adding image fileName and fileExtension). Previously,
i wasn't getting any error. Data was inserted into database except that CurrentOrg was left blank. I dont know wats the problem
So, this is the code and output:
12 : .jpg - 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 : .jpg - 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,fileExtension,Id); Line 131: Response.Redirect("~/Members/Display.cshtml");
Line 132: }
@{//Checking for user log-inif(!WebSecurity.IsAuthenticated){Response.Redirect("~/Account/Login1");}//..........Layout="~/_Layout.cshtml";Page.Title="Edit a Record";//Image.....WebImagephoto=null;varnewFileName="";varimagePath="";varimageThumbPath="";varfileTitle="";varfileExtension="";varfname="";varlname="";varinstitu="";varCourse="";varadmission="";varpassOut="";varId="";varcuLoc="";varcuOrg="";varphone="";varmobile="";vardate=Request.Form["Date"];varmonth=Request.Form["Month"];varyear=Request.Form["Year"];varbirthdate=String.Concat(date,"/",month,"/",year);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();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()){vardb=Database.Open("Alumni");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=@9,ImageName=@10,Extension=@11 WHERE UserId=@12";db.Execute(updateCommand,fname,lname,institu,Course,admission,passOut,birthdate,cuLoc,cuOrg,phone,mobile,fileTitle,fileExtension,Id);Response.Redirect("~/Members/Display.cshtml");}}}<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><inputtype="hidden"name="ID"/></br></br><labelfor="Image">Image</label><br><inputtype="file"name="Image"/><div><inputtype="submit"value="submit"/></div></fieldset></form><p><ahref="~/Members/Edit.cshtml">Return Records listing</a></p>
newFileName=Guid.NewGuid().ToString()+"_"+Path.GetFileName(photo.FileName).ToString(); // to no effect, also declared newFileName as String
getting error:
11 : 82a3a6c7-82bf-4ffd-a3c3-87ce13237804_Photo0015.jpg - 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: 11 : 82a3a6c7-82bf-4ffd-a3c3-87ce13237804_Photo0015.jpg - Input string was not in a correct format.
Source Error:
Line 130: var db = Database.Open("Alumni");
Line 131: 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 WHERE UserId=@11";
Line 132: db.Execute(updateCommand, fname, lname,institu, Course, admission,passOut,birthdate,cuLoc,cuOrg, phone, mobile,newFileName,Id); Line 133: Response.Redirect("~/Members/Display.cshtml");
Line 134: }
Srry to repost but this is updated page code where i have removed fileName and fileExtension & replaced with ImageName in db and newFileName as variable.
@{
//Checking for user log-in
if (!WebSecurity.IsAuthenticated)
{
Response.Redirect("~/Account/Login1");
}
//..........
Layout = "~/_Layout.cshtml";
Page.Title = "Edit a Record";
//Image.....
WebImage photo = null;
String newFileName = "";
var imagePath = "";
var imageThumbPath = "";
//String fileTitle = "";
// String 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).ToString();
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().ToString();
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 WHERE UserId=@11";
db.Execute(updateCommand, fname, lname,institu, Course, admission,passOut,birthdate,cuLoc,cuOrg, phone, mobile,newFileName,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>
This is dam frustrating.. . I tried and checked everything possible and as i m a beginner my scope
is limited! I am not able to find out what exactly is causing this problem! Can there be a bug in webmatrix? Or any other code is conflicting with the logic or anything... else happenning?
Problem1: Image (Unsolved)
Problem2 : CuOrg i.e a field that doesnt get updated/inserted in databse even when insert/update query is fired and this field has a validator so user cannot keep it empty. (unsolved).
You need to recheck your line of code. The main thing in your work is the correct code!
The image will be saved in your directory. Just rewrite the code. I would prefer your try everything in a seperate file!(Page)
Than your INSERT statement should be done in the database workspace. In the database workspace double click a database it will connect. Than in the top bar select New Query just write the word to execute and not use any variable do it like
select * from userprofile where userid = 29
and press execute. This will give you the result Also you can try out insertion to check which code is working good. Than paste code in your file
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
Red77
Member
1 Points
26 Posts
Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015.jpg ...
Dec 25, 2012 05:45 PM|LINK
I am a beginner to ASP.Net. Creating my 1st site with razor syntax. No previous knowledge about ASP. So I m learning about it right now by modifying the template sites. Any modifications in the existing code to optimize it is welcomed.
Here's the Problem:
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: }Stack Trace:
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 25, 2012 06:05 PM|LINK
This error is because the variable you are using has some null value while the database table you are accessing does not allows nulls
Somehow it can also be because sometime user input data as a string while the data in database is datetime. Your user may wants to input 12 while the database needs something else.
You need to execute data. And the 12 you are entering is somewhat an interger. Try covering the variable and converting to string() like
This would do it.
~~! FIREWALL !~~
Red77
Member
1 Points
26 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 25, 2012 06:26 PM|LINK
Well, the real problem is the Image name to be inserted into database. This is one problem.
Second problem is of CurrentOrganization. My Database has this column "CurrentOrg" which does allow nulls!
Problem 1: When i insert image fileName or fileExtension, it gives error String not in proper format. Both the columns for these are allowing null values. I m using Guid which is converted to string... prefixed to filename of image. Whats the problem?
Problem 2: CurrentOrg in database is Null while all records are being inserted/Updated. If there is any problem with CurrentOrg about String format, it should give me this error that i m getting now(After adding image fileName and fileExtension). Previously, i wasn't getting any error. Data was inserted into database except that CurrentOrg was left blank. I dont know wats the problem
So, this is the code and output:
12 : .jpg - 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 : .jpg - 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,fileExtension,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:
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 25, 2012 06:54 PM|LINK
1. Try using .ToString() at end of the imageName
2. Check whether your column is having the exact value you want to be inserted?
Try doing like
If this also does not work. Convert your Image_Name to a String() too
~~! FIREWALL !~~
Red77
Member
1 Points
26 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 25, 2012 08:00 PM|LINK
I did try all that stuff. I used this code:
getting error:11 : 82a3a6c7-82bf-4ffd-a3c3-87ce13237804_Photo0015.jpg - 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: 11 : 82a3a6c7-82bf-4ffd-a3c3-87ce13237804_Photo0015.jpg - Input string was not in a correct format.
Source Error:
Line 130: var db = Database.Open("Alumni"); Line 131: 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 WHERE UserId=@11"; Line 132: db.Execute(updateCommand, fname, lname,institu, Course, admission,passOut,birthdate,cuLoc,cuOrg, phone, mobile,newFileName,Id); Line 133: Response.Redirect("~/Members/Display.cshtml"); Line 134: }Source File: c:\Users\XBlaZE\Desktop\Project-X\Test1\Members\Update.cshtml Line: 132
Stack Trace:
@{ //Checking for user log-in if (!WebSecurity.IsAuthenticated) { Response.Redirect("~/Account/Login1"); } //.......... Layout = "~/_Layout.cshtml"; Page.Title = "Edit a Record"; //Image..... WebImage photo = null; String newFileName = ""; var imagePath = ""; var imageThumbPath = ""; //String fileTitle = ""; // String 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).ToString(); 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().ToString(); 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 WHERE UserId=@11"; db.Execute(updateCommand, fname, lname,institu, Course, admission,passOut,birthdate,cuLoc,cuOrg, phone, mobile,newFileName,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>Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 25, 2012 08:02 PM|LINK
The input is in other format.
You sure that your database column is having datatype set to nvarchar? Please see it again.
~~! FIREWALL !~~
Red77
Member
1 Points
26 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 25, 2012 08:03 PM|LINK
Yes Sure it is!
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 25, 2012 08:06 PM|LINK
According to the code, everything should work fine.
However please have a look on your variables because an invalid, or an empty variable executes this error.
~~! FIREWALL !~~
Red77
Member
1 Points
26 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 25, 2012 08:22 PM|LINK
This is dam frustrating.. . I tried and checked everything possible
and as i m a beginner my scope
is limited! I am not able to find out what exactly is causing this problem! Can there be a bug in webmatrix? Or any other code is conflicting with the logic or anything... else happenning?
Problem1: Image (Unsolved)
Problem2 : CuOrg i.e a field that doesnt get updated/inserted in databse even when insert/update query is fired and this field has a validator so user cannot keep it empty. (unsolved).
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 25, 2012 08:29 PM|LINK
No there is no bug. But a bug in your code.
Sometimes other developers get this too.
You need to recheck your line of code. The main thing in your work is the correct code!
The image will be saved in your directory. Just rewrite the code. I would prefer your try everything in a seperate file!(Page)
Than your INSERT statement should be done in the database workspace. In the database workspace double click a database it will connect. Than in the top bar select New Query just write the word to execute and not use any variable do it like
and press execute. This will give you the result Also you can try out insertion to check which code is working good. Than paste code in your file
~~! FIREWALL !~~