@{
//Checking for user log-in
if (!WebSecurity.IsAuthenticated)
{
Response.Redirect("~/Account/Login1");
}
//..........
Layout = "~/_Layout.cshtml";
Page.Title = "Edit a Record";
//Removed this variable declaration section.
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);
var dbCommand = "Select * From Info WHERE UserId = 7"; //This is the changed code.
var row = db.QuerySingle(dbCommand); //This is changed code.
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");
}
}
}
This does not return any record on current page. Says no student selected(Validator error msg). So the rows are null. Means QuerySingle returned no values.
I m working in WebMatrix and using its IIS Server.
Afzaal.Ahmad.Zeeshan
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
I cant see anything in Database workspace to try any query.. or i didn't get ur statement.
int date = Request.Form["Date"];
int month = Request.Form["Month"];
int year = Request.Form["Year"];
DateTime birthdate = new DateTime(year,month,date);
Programming to simplify, dont look for hard way
Suwandi - Non Graduate Programmer
Also, you can see which ones are missing when you don't expect them to be. cuOrg and cuLoc will not exist because when you assign a value to them, you have a typo in the Request item you are referencing
Thanks for that typo ! Now this problem is solved!I tested by rendering variables and all
contained values:
Admin SuperUser Institute of Technology Computer Engg 2010 2013 15/January/2222 Earth //CurrentLoction Tonic //(CurrentOrg Solved) 44444 322211 9952807f-1ef6-47ef-82a6-f160d829c071_Photo0015.jpg //Problem for string format when updating/inserting into database.
But when i Update into database, it shows this String not in correct format error:
11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_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 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_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: @fname<br />
You have repeated @9 in your parameters - you use it to update Phone AND mobile, then keep going with 10. That means that the photo file name value is being passed to the Id column, which is clearly wrong. Try the following:
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=@10,ImageName=@11 WHERE UserId=@12";
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=@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");}
Still getting an error...
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 129: var db = Database.Open("Alumni");
Line 130: 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=@10,ImageName=@11 WHERE UserId=@12";
Line 131: //Error here db.Execute(updateCommand, fname, lname,institu, Course, admission,passOut,birthdate,cuLoc,cuOrg, phone, mobile,newFileName,Id);
Line 132: Response.Redirect("~/Members/Display.cshtml");
Line 133:
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:58 PM|LINK
Did u mean doing this?
@{ //Checking for user log-in if (!WebSecurity.IsAuthenticated) { Response.Redirect("~/Account/Login1"); } //.......... Layout = "~/_Layout.cshtml"; Page.Title = "Edit a Record"; //Removed this variable declaration section. 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); var dbCommand = "Select * From Info WHERE UserId = 7"; //This is the changed code. var row = db.QuerySingle(dbCommand); //This is changed code. 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"); } } }This does not return any record on current page. Says no student selected(Validator error msg). So the rows are null. Means QuerySingle returned no values.
I m working in WebMatrix and using its IIS Server.
I cant see anything in Database workspace to try any query.. or i didn't get ur statement.
Afzaal.Ahmad...
Contributor
2759 Points
1060 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 25, 2012 09:03 PM|LINK
You didnt get it. Go in the database workspace, and in the options there is a link for a New Query in the upper bar!
And the error is in your code then. You need to check over all databases. Because the code is giving NO result!
~~! FIREWALL !~~
oned_gk
All-Star
35908 Points
7338 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 26, 2012 12:29 AM|LINK
try this
int date = Request.Form["Date"]; int month = Request.Form["Month"]; int year = Request.Form["Year"]; DateTime birthdate = new DateTime(year,month,date);Suwandi - Non Graduate Programmer
Mikesdotnett...
All-Star
155597 Points
19981 Posts
Moderator
MVP
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 26, 2012 07:08 AM|LINK
An easy way to debug this is to actually render the variables you are passing to the SQL onto the screen:
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"); @fname<br /> @lname<br /> @institu<br /> @Course<br /> @admission<br /> @passOut<br /> @birthdate<br /> @cuLoc<br /> @cuOrg<br /> @phone<br /> @mobile<br /> @fileTitle<br /> @Id<br /> }Then you can see which values doen't look right.
Also, you can see which ones are missing when you don't expect them to be. cuOrg and cuLoc will not exist because when you assign a value to them, you have a typo in the Request item you are referencing
That should be
Web Pages CMS | My Site | Twitter
Red77
Member
1 Points
26 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 26, 2012 08:14 AM|LINK
Thanks for that typo
! Now this problem is solved!I tested by rendering variables and all
contained values:
Admin
SuperUser
Institute of Technology
Computer Engg
2010
2013
15/January/2222
Earth //CurrentLoction
Tonic //(CurrentOrg Solved)
44444
322211
9952807f-1ef6-47ef-82a6-f160d829c071_Photo0015.jpg //Problem for string format when updating/inserting into database.
But when i Update into database, it shows this String not in correct format error:
11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_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 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_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: @fname<br />Stack Trace:
Mikesdotnett...
All-Star
155597 Points
19981 Posts
Moderator
MVP
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 26, 2012 08:56 AM|LINK
You have repeated @9 in your parameters - you use it to update Phone AND mobile, then keep going with 10. That means that the photo file name value is being passed to the Id column, which is clearly wrong. Try the following:
Web Pages CMS | My Site | Twitter
Red77
Member
1 Points
26 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 26, 2012 09:25 AM|LINK
Still getting an error...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 129: var db = Database.Open("Alumni"); Line 130: 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=@10,ImageName=@11 WHERE UserId=@12"; Line 131: //Error here db.Execute(updateCommand, fname, lname,institu, Course, admission,passOut,birthdate,cuLoc,cuOrg, phone, mobile,newFileName,Id); Line 132: Response.Redirect("~/Members/Display.cshtml"); Line 133:Afzaal.Ahmad...
Contributor
2759 Points
1060 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 26, 2012 10:27 AM|LINK
As Mikesdotnetting told you for working with the variables, that was fine and a best answer.
This error is still because of the wrong type of data being inserted to the row
If the datatype of row in int and you want to even add a alphabet you will get this error. Please convert the data to the type of database table!
~~! FIREWALL !~~
Red77
Member
1 Points
26 Posts
Re: Input string was not in a correct format.11 : 5ac4b102-080a-495f-b469-2d1bfbf7c0c3_Photo0015....
Dec 26, 2012 12:51 PM|LINK
Got My problem solved!
It was the Id field that was empty! It was requested using querystring
in (!IsPost).
It was again to be requested in (IsPost){} to execute Update querry where UserId = Id(Which was empty as it was not requested).
This is my version of answer. Is it right?