Wednesday, August 24, 2011

Displaying Error/Status Messages in our Web Form ASP.Net using XML file.

Hi Friends,We knew that,WebPages must be designed and write code in a secure manner.

Today i am going to explain about how to display the Alert /Status / Error messages in ASP.Net using XML File.
In Most of our web pages, we must use some labels or alert messages for user status or for user friendliness. After looking at that Alert/Error/Status messages itself User has to go/move forward into further pages in our web pages.

Raw Data(Direct Data) should not be used directly.It is not the good practice for that page as well as developer too.
In place of the Raw Data, We use one XML file to stored all the Error/Alert/Status Messages in a secured way and Display them in our Web Page.

Implementation:

I will Explain this concept by creating a Login Page in a simple way.

First of All, Whatever Error/Alert/Status messages which you want to display in your Web Page must be written in one XML file named as
"AlertorErrorMessages.xml".

AlertorErrorMessages.xml:
===================






In above xml file you can add number of messages in tag under the key,value pair.
After this, Open the Web.Config file, and write the code below

Web.config:
=========
Under Configuration Section




In above Web.Config file, "value" is given as your xml file path.

After that,Create one Web Form (LoginPage.aspx) , write the code below.

LoginPage.aspx:
============




Login ID :
Password :
Code Behind For the above page is:

LoginPage.aspx.cs:
=============
You must add the following namespace for Configuration Settings.

using System.Configuration;

public class LoginPage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) 
{
string statusMessage=ConfigurationManager.AppSettings.Get("ForStatusMessage").ToString( );               
}
protected void btnSubmit_Click(object sender,EventArgs e)
{ 
lbl_Status.Visible=True; //After Clicking the button itself we must visible the label to display message.
AppGetXMLData  agxd= new AppGetXMLData(statusMessage);    //Seperate class, where the actual XML data manipulation placed.
//You can get the login credentials from here to connect with DB.
//Here i just simply take the login name as "BhaskarReddy" and password name as "Symphony"
if(txt_LoginId.Text= ="BhaskarReddy" && txt_Password.Text= ="Symphony")  
{
lbl_Status.Text=agxd.ValidationXMLKey("LoginSuccess");
}
else
{
lbl_Status.Text=agxd.ValidationXMLKey("LoginFails");
}
}
}

Create a class to implement the ValidationXMLKey functionality:
(App_Code/AppGetXMLData.cs):
=========================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Xml;
using System.ServiceModel.Dispatcher;

public class AppGetXMLData
{
XmlDocument doc;
ExceptionHandler objExp;
public AppGetXMLData()
{
}

public AppGetXMLData(string FileName)
{
doc = new XmlDocument();
doc.Load(FileName);
}
public string ValidationXMLKey(string TagName)
{
string constString = string.Empty;
try
{
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;
    }
}

Compile and Run this page.

If you enter the Correct LoginCredentials LoginId: BhaskarReddy Password: Symphony Then Click the Submit button, then you find the Status Message like.You are authorized to move forward with our site If you enter wrong credentials then your Status Message like You are unauthorized user! check once or Register again
That's It :)
Happy Coding 
Thanks 
BhaskarReddy

Labels: , , , , ,

2 Comments:

At September 3, 2011 at 1:34 PM , Anonymous Anonymous said...

Very Clear Explaination
Simple and Useful
Good Work

 
At September 8, 2011 at 11:47 PM , Anonymous Anonymous said...

Great Job
Thanks

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home