Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 01, 2012 07:05 PM by UFO Disko
Member
51 Points
50 Posts
Mar 01, 2012 05:37 PM|LINK
Hello again,
I'm doing the tutorials here on the site and they're pretty good. But I'm having problems.
I just created an AddMovie.cshtml with this code
@{ Layout = "~/_SiteLayout.cshtml"; Page.Title = "Add Movie!"; var filmName = ""; var filmGenre = ""; if(IsPost) { filmName = Request["formName"]; filmGenre = Request["formGenre"]; } var SQLINSERT = "INSERT INTO Names (MovieName, MovieGenre) VALUES (@0, @1)"; var db = Database.Open("Movies"); db.Execute(SQLINSERT, filmName, filmGenre); //Response.Redirect("Default.cshtml"); } <form method="post" action=""> <p>Name: <label for="formName"></label> <input type="text" name="formName" id="formName" /> </p> <p>Genre: <label for="formGenre"></label> <input type="text" name="formGenre" id="formGenre" /> </p> <p> <input type="submit" value="Add Movie" /> </p> </form>
The problem here, when I add a record to the database it gets submitted with another empty row.
And if I enable Response.Redirect it just keeps on submitting empty rows until I close the window.
Please advise.
Contributor
3964 Points
999 Posts
Mar 01, 2012 06:33 PM|LINK
That can't be the complete code.
What are the values of @0 and @1?
If you hardcode values in there, your query should work fine: var SQLINSERT = "INSERT INTO Names (MovieName, MovieGenre) VALUES (Titanic, Comedy)";
Mar 01, 2012 06:47 PM|LINK
I can assure you it's the full code, I just followed this tutorial
http://www.asp.net/web-pages/videos/introduction/create-a-web-interface-in-webmatrix
@0 = MovieName
@1 = MovieGenre
At least that's how the tutorial explained it.
All-Star
154955 Points
19872 Posts
Moderator
MVP
Mar 01, 2012 06:48 PM|LINK
You need to move the code that inserts into the database inside the IsPost block:
@{ Layout = "~/_SiteLayout.cshtml"; Page.Title = "Add Movie!"; var movieName = ""; var movieGenre = ""; if(IsPost) { movieName = Request["formName"]; movieGenre = Request["formGenre"]; var SQLINSERT = "INSERT INTO Names (MovieName, MovieGenre) VALUES (@0, @1)"; var db = Database.Open("Movies"); db.Execute(SQLINSERT, movieName, movieGenre); //Response.Redirect("AddMovie.cshtml"); } } <form id="form1" name="form1" method="post" action=""> <p>Name: <label for="formName"></label> <input type="text" name="formName" id="formName" /> </p> <p>Genre: <label for="formGenre"></label> <input type="text" name="formGenre" id="formGenre" /> </p> <p> <input type="submit" name="submit" id="submit" value="Submit" /> </p> </form>
Mar 01, 2012 07:05 PM|LINK
Thank you very much, that solved it.
EDIT: I solved the delete problem :)
UFO Disko
Member
51 Points
50 Posts
SQL Insert is submitting empty rows
Mar 01, 2012 05:37 PM|LINK
Hello again,
I'm doing the tutorials here on the site and they're pretty good. But I'm having problems.
I just created an AddMovie.cshtml with this code
@{ Layout = "~/_SiteLayout.cshtml"; Page.Title = "Add Movie!"; var filmName = ""; var filmGenre = ""; if(IsPost) { filmName = Request["formName"]; filmGenre = Request["formGenre"]; } var SQLINSERT = "INSERT INTO Names (MovieName, MovieGenre) VALUES (@0, @1)"; var db = Database.Open("Movies"); db.Execute(SQLINSERT, filmName, filmGenre); //Response.Redirect("Default.cshtml"); } <form method="post" action=""> <p>Name: <label for="formName"></label> <input type="text" name="formName" id="formName" /> </p> <p>Genre: <label for="formGenre"></label> <input type="text" name="formGenre" id="formGenre" /> </p> <p> <input type="submit" value="Add Movie" /> </p> </form>The problem here, when I add a record to the database it gets submitted with another empty row.
And if I enable Response.Redirect it just keeps on submitting empty rows until I close the window.
Please advise.
adamturner34
Contributor
3964 Points
999 Posts
Re: SQL Insert is submitting empty rows
Mar 01, 2012 06:33 PM|LINK
That can't be the complete code.
What are the values of @0 and @1?
If you hardcode values in there, your query should work fine: var SQLINSERT = "INSERT INTO Names (MovieName, MovieGenre) VALUES (Titanic, Comedy)";
UFO Disko
Member
51 Points
50 Posts
Re: SQL Insert is submitting empty rows
Mar 01, 2012 06:47 PM|LINK
I can assure you it's the full code, I just followed this tutorial
http://www.asp.net/web-pages/videos/introduction/create-a-web-interface-in-webmatrix
@0 = MovieName
@1 = MovieGenre
At least that's how the tutorial explained it.
Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: Insert into db error
Mar 01, 2012 06:48 PM|LINK
You need to move the code that inserts into the database inside the IsPost block:
@{ Layout = "~/_SiteLayout.cshtml"; Page.Title = "Add Movie!"; var movieName = ""; var movieGenre = ""; if(IsPost) { movieName = Request["formName"]; movieGenre = Request["formGenre"]; var SQLINSERT = "INSERT INTO Names (MovieName, MovieGenre) VALUES (@0, @1)"; var db = Database.Open("Movies"); db.Execute(SQLINSERT, movieName, movieGenre); //Response.Redirect("AddMovie.cshtml"); } } <form id="form1" name="form1" method="post" action=""> <p>Name: <label for="formName"></label> <input type="text" name="formName" id="formName" /> </p> <p>Genre: <label for="formGenre"></label> <input type="text" name="formGenre" id="formGenre" /> </p> <p> <input type="submit" name="submit" id="submit" value="Submit" /> </p> </form>Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
UFO Disko
Member
51 Points
50 Posts
Re: SQL Insert is submitting empty rows
Mar 01, 2012 07:05 PM|LINK
Thank you very much, that solved it.
EDIT: I solved the delete problem :)