I'm trying to add a comment section for my webpage. To do this, I've set up a table in my .mdf database named Comments.
The columns are ID (prim. key), MetricName, Time, Name, Email, Comment and Likes.
I've written the form:
@using WebMatrix.Data;
@{//title of tab
ViewBag.Title = "Comments";
}
@{
var name = ""; var email = ""; var comment = ""; DateTime time = DateTime.Now; var metric = ""; int likes = 0;
if(IsPost){
name = Request.Form["name"];
if (name == ""){
name = "Anonymous";
}
email = Request.Form["email"];
comment = Request.Form["comment"];
time = DateTime.Now;
metric = "Merch/Cust Reported Bugs";
var db = Database.Open("Metrics");
var insertCommand = "INSERT INTO Comments (MetricName, Time, Name, Email, Comment, Likes) Values(@metric, @time, @name, @email, @comment, @likes)";
db.Execute(insertCommand, metric, time, name, email, comment, likes);
Response.Redirect("~/Comments");
}
}
<h1>Add a comment</h1>
<form action ="" method="post">
<fieldset>
<legend>Comment</legend>
<label for="name">Name:</label>
<input type="text" name="name" value="@Request.Form["name"]" />
<label for="email">Email:</label>
<input type="text" name="email" value="@Request.Form["email"]" />
<br />
<label for="comment">Comment:</label>
<textarea rows="3" cols="40" name = "comment" value="@Request.Form["comment"]"></textarea>
<p><input type="submit" name="buttonsubmit" value="Add Comment" /></p>
</fieldset>
</form>
I'm really new to database stuff, and have no idea what I'm doing, but I'm getting this error when I try adding a comment:
Quote
Must declare the scalar variable "@metric".
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.Data.SqlClient.SqlException: Must declare the scalar variable "@metric".
Source Error:
Line 21: var db = Database.Open("Metrics");
Line 22: var insertCommand = "INSERT INTO Comments (MetricName, Time, Name, Email, Comment, Likes) Values(@metric, @time, @name, @email, @comment, @likes)";
Line 23: db.Execute(insertCommand, metric, time, name, email, comment, likes);
Line 24: Response.Redirect("~/Comments");
Line 25: }
Source File: c:\Users\rflowers\Documents\Visual Studio 2010\Projects\L1DashboardRedone\L1DashboardRedone\Views\Home\Comments.cshtml Line: 23
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.Data.SqlClient.SqlException: Must declare the scalar variable "@metric".
Source Error:
Line 21: var db = Database.Open("Metrics");
Line 22: var insertCommand = "INSERT INTO Comments (MetricName, Time, Name, Email, Comment, Likes) Values(@metric, @time, @name, @email, @comment, @likes)";
Line 23: db.Execute(insertCommand, metric, time, name, email, comment, likes);
Line 24: Response.Redirect("~/Comments");
Line 25: }
Source File: c:\Users\rflowers\Documents\Visual Studio 2010\Projects\L1DashboardRedone\L1DashboardRedone\Views\Home\Comments.cshtml Line: 23

New Topic/Question
Reply



MultiQuote




|