im using mvc2 application..i give required field validator for textbox but it not fire when textbox is empty.the error occur like
"The parameterized query '(@Name varchar(10),@Email varchar(20),@Comment varchar(10))inser' expects the parameter '@Name', which was not supplied."
And also error value is saved in database even validation raise(Name=785 is saved in db but i fix the validation for name is must be character.this validation raise when we give number in that textbox but saved into db
My code:
Studentcontroller.cs
public class StudentController : Controller
{
public ActionResult GetStudent()
{
return View();
}
[HttpPost]
public ActionResult GetStudent(StudentModel model)
{
SqlConnection connObj = new SqlConnection();
Response.Write(model.Name + model.Comment + model.Email);
connObj.ConnectionString = "Data Source=MOB-PERLZ-WS192;User ID=sa;Password=Mobius@123;Initial Catalog=MVC";
connObj.Open();
SqlCommand comm = new SqlCommand("insert into student(Name,Email,Comment) values(@Name,@Email,@Comment)", connObj);
comm.Parameters.Add("@Name", SqlDbType.VarChar, 10).Value = model.Name;
comm.Parameters.Add("@Email", SqlDbType.VarChar, 20).Value = model.Email;
comm.Parameters.Add("@Comment", SqlDbType.VarChar, 10).Value = model.Comment;
int result = comm.ExecuteNonQuery();
if (result != 0)
Response.Write(" added");
else
Response.Write("Error");
return View();
}
}<pre lang="c#">
GetStudent.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>GetStudent</h2>
<% Html.EnableClientValidation(); %>
<% using (Html .BeginForm() ){%>
<%-- <%= Html.ValidationSummary(true, "A few fields are still empty") %>--%>
<fieldset>
<legend>Student Detail</legend>
<div class="editor-label">
<%= Html.LabelFor(model=>model .Name ) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Name)%>
<%= Html.ValidationMessageFor(model =>model .Name ) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model =>model .Email ) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Email)%>
<%=Html.ValidationMessageFor(model => model.Email)%>
</div>
<div class="editor-label">
<%= Html.LabelFor(model =>model .Comment ) %>
</div>
<div class="editor-field">
<%= Html.TextAreaFor(model => model.Comment, 10, 25, null)%>
<%= Html.ValidationMessageFor(model => model.Comment)%>
</div>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
<p id="result"><%=TempData["Message"] %></p>
<% } %>
</asp:Content>
StudentModel.cs
public class StudentModel
{
[Required]
[StringLength(10, ErrorMessage = "must be under 10")]
[RegularExpression("^([a-zA-z\\s]{4,32})$", ErrorMessage = "Only Alphabets are allowed")]
[DisplayName("Name")]
public string Name { get; set; }
[Required(ErrorMessage = "* Required")]
[DataType(DataType.EmailAddress, ErrorMessage = "Your email address contains some errors")]
[DisplayName("Email address")]
[RegularExpression("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", ErrorMessage = "Enter a Valid Email")]
public string Email { get; set; }
[Required ]
public string Comment { get; set; }
}