ASP.NET Articles

ASP.NET , C# – Configuring to use XML to display error and validation message

ASP.NET , C# – Configuring to use XML to display error and validation message

Step 1 – Add XML file to store error and validation message.
              Below is sample of ErrorMessage.xml :
<?xml version=1.0 encoding=utf-8 ?>
<appSettings>
<add key=NoMatchingRecord value=No Matching Records Found/>
<appSettings>                  

Below is sample of ValidationMessage.xml :

<?xml version=”1.0″ encoding=”utf-8″ ?>
<appSettings>

 <add key=”EndDateGreaterthanStartDate” value=”End Date should be greater than Start Date”/>
</appSettings>

If more error or validation message need to be added then add it to ErrorMessage.xml or ValidationMessage.xml with key and value pair. Also , make sure key is unique and not repeated in XML file. You can also add more XML files if needed

Step 2 – Add below key in appSettings section of Web.config file
<appSettings>
  <add key=”ErrorMessage” value=”D:ConfigErrorMessage.xml”></add>
  <add key=”ValidationMessage” value=”D:ConfigValidationMessage.xml”></add>
 </appSettings>

Above keys are added which contains path of XML file as  key value

Step 3 – Add a Class file and name it to CustomMessage.cs . Add below code in Custom Message.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
////// <summary> Summary description for CustomMessage/// </summary>
public class CustomMessage{
System.Xml.XmlDocument doc;

{
}
public CustomMessage(String FileName)
{
doc = new XmlDocument();
doc.Load(FileName);
}

/// <summary>/// To get value from XML file at specified location./// </summary>/// <param name=”TagName”></param>/// <returns></returns>

public string GetMessageValue(string TagName)
    {
        string ConstString = string.Empty;
        try
        {
            //Get Connection path from XML file

            XmlNodeList elemList = doc.GetElementsByTagName(“add”);
            for (int i = 0; i < elemList.Count; i++)
            {
                string KeyName = elemList[i].Attributes[“key”].Value;
                if (KeyName == TagName)
                {
                    ConstString = elemList[i].Attributes[“value”].Value;
                }
            }
        }
        catch (Exception ex)
        {
        }
        return ConstString;
    }

}

CustomMessage class contains a public constructor which accepts FileName as input paramter and load the XML file into XMLDocument.

CustomMessage class also contains a method GetMessageValue which takes key as input parameter and returns value of key.

Step 4 – Final step is to consume the CustomMessage class to display appropriate message. Below is code in calling aspx or cs file :

string errorFile = ConfigurationManager.AppSettings.Get(“ErrorMessage”).ToString();
CustomMessage objCustomMessage = new CustomMessage(errorFile);

this.lblError.Text = objCustomMessage.GetMessageValue(“NoMatchingRecord”);

Line 1 is used to get path of ErrorMessage.xml
Line 2 is used to call Custom message class constructor to load XML file into XMLDocument
Line 3 is used to get value of error message key by passing key to CustomMessage method GetMessageValue

string validationFile = ConfigurationManager.AppSettings.Get(“ValidationMessage”).ToString();
CustomMessage objCustomMessage = new CustomMessage(validationFile);this.lblValidation.Text = objCustomMessage.GetMessageValue(“EndDateGreaterthanStartDate”);  

 Line 1 is used to get path of ValidationMessage.xml
 Line 2 is used to call Custom message class constructor to load XML file into XMLDocument
 Line 3 is used to get value of validation message key by passing key to CustomMessage method GetMessageValue

Leave a Reply