Menu

Article Over An exception with required field validator

Sharing is caring!

Error message : WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery. Please add a ScriptResourceMapping named jquery(case-sensitive).

This article will give you the complete guidance about how to remove such kind of errors that is title of article in .Net Framework 4.5. This error comes if you are going to use the RequiredFieldValidator control on any of the server control. Below is complete guidance with examples

Create Web Form
Here is complete code of designing of aspx page.

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Home.aspx.cs” Inherits=”Home” %>

<!DOCTYPE html>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title></title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

Employee id <asp:TextBox ID=”employee_id” runat=”server” ValidateRequestMode=”Enabled”></asp:TextBox>

<asp:RequiredFieldValidator ID=”validate_employee_id” runat=”server” ControlToValidate=” employee_id ” ErrorMessage=”Empid Can not be left blank” ForeColor=”#CC0000″></asp:RequiredFieldValidator>

<br />

<br />

Employee Name:&nbsp;<asp:TextBox ID=”employee_name” runat=”server”></asp:TextBox>

<br />

<br />

Designation:&nbsp;<asp:TextBox ID=”employee_designation” runat=”server”></asp:TextBox>

<br />

<br />

Company:&nbsp;<asp:TextBox ID=”employee_company” runat=”server”></asp:TextBox>

&nbsp;<br />

<br /> <asp:Button ID=”save” runat=”server” OnClick=”save_Click” Text=”Click to Submit” Height=”27px” Width=”66px” />

&nbsp;&nbsp;

<asp:Button ID=”fetch” runat=”server” Text=”Retrieve” Height=”26px” Width=”62px” OnClick=”fetch_Click” />

<br />

<br />

</div>

</form>

</body>

</html>

 Server Side Code

You need to create a click event for your button control that shows input values.

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class Home : System.Web.UI.Page

{

   SqlConnection mycon = new SqlConnection(ConfigurationManager.ConnectionStrings[“conn1”].ConnectionString);

   SqlCommand mycommand;

   SqlDataReader mydr;

   protected void Page_Load(object sender, EventArgs e)

   {

   }

   protected void save_Click(object sender, EventArgs e)

   {

       try

       {

           mycommand = new SqlCommand(“insert into employeedetails values(@id,@name,@designation,@company)”, mycon);

           mycommand.Parameters.Add(“@id”, SqlDbType.NVarChar).Value = employee_id.Text;

           mycommand.Parameters.Add(“@name”, SqlDbType.NVarChar).Value = employee_name.Text;

           mycommand.Parameters.Add(“@designation”, SqlDbType.NVarChar).Value = employee_designation.Text;

           cmyommand.Parameters.Add(“@company”, SqlDbType.NVarChar).Value = employee_company.Text;

           mycon.Open();

            if (Convert.ToBoolean(command.ExecuteNonQuery()))

           {

               Response.Write(“<script>alert(‘Record Successfully Inserted’);</script>”);

           }

           else

           {

               Response.Write(“<script>alert(‘Record could not be Inserted’);</script>”);

           }

       }

       catch (Exception ex)

       {

           Response.Write(ex.Message);

       }

       finally

       {

           mycon.Close();

       }

   }

   protected void fetch_Click(object sender, EventArgs e)

   {

       try

       {

           mycommand = new SqlCommand(“select * from employeedetails where empid=@empid”, mycon);

           command.Parameters.Add(“@empid”, SqlDbType.NVarChar).Value = employee_id.Text;

           con.Open();

           mydr = command.ExecuteReader();

           DataTable mydt = new DataTable();

           mydt.Load(mydr);

           if (mydt.Rows.Count > 0)

           {

               Employee_name.Text = mydt.Rows[0][“name”].ToString();

               Employee_designation.Text = mydt.Rows[0][“Designation”].ToString();

               Employee_company.Text = mydt.Rows[0][“Company”].ToString();

           }

       }

       catch (Exception ex)

       {

           Response.Write(ex.Message);

       }

       finally

       {

           con.Close();

       }

   }

}

Now Run the Application

Now run your website and leave employee id field blank and click on submit button. You

will get below error message as figure 1.1.

.Net Training (SSDN Technologies)

Figure 1.1 : Error Message After submit button click

Resolve Error

Now we will add a key for Unobtrusive Validation Mode under <appSettings> tag in web.config file as shown in below code.

<?xml version=”1.0″?>

<!–

For more information on how to configure your ASP.NET application, please visit

http://go.microsoft.com/fwlink/?LinkId=169433

–>

<configuration>

<connectionStrings>

   <add name=”conn1″ connectionString=”Data Source=ANKIT\SQLEXPRESS;Initial Catalog=db1;User Id=sa;Password=1234″/>

</connectionStrings>

<system.web>

   <compilation debug=”true” targetFramework=”4.5″/>

   <httpRuntime targetFramework=”4.5″/>

</system.web>

<appSettings>

   <add key=”ValidationSettings:UnobtrusiveValidationMode” value=”None” />

</appSettings>

</configuration>

Validation Message

After that again run the same page and leave blank Employee_id field and click on submit button. Now you will get a validation message as shown in figure 1.2

.Net Training (SSDN Technologies 2)

                                                                   Figure 1.2 Validation message

Now insert value in employee id field and run the application again. Now You will get desired result as shown in figure 1.3.

.Net Training (SSDN Technologies 23

42 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

shares