Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
public class StudentController : Controller
    {
        
        //
        // GET: /Student/

        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



XML
<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


C#
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(ErrorMessage = "* Required")]

       [Required ]
       public string Comment { get; set; }



   }
Posted
Updated 25-Oct-12 20:43pm
v2

1 solution

Yes, because in Controller you are not checking whether your page is valid or not.
Try reading A sample on ASP.NET MVC Model Validation using DataAnnotations and ModelValidators[^]. This will help you to guide through.


--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900