Friday, April 22, 2011

Handling special characters in XML strings in C#.Net

There are set of characters that cannot be used in normal in XML a strings. If there are any of these characters in a xml string it will break xml parsing. But if you still want to include these characters in xml string we have to replace those characters according to the following list. 

















If you need to construct xml strings in a C# program you can escape those characters using
SecurityElement.Escape(XmlValue) method given by the .Net framework. 











Using above method you can easily pass those special charactors via xml string without any trouble. 


Tuesday, April 5, 2011

LINQ to XML - Samples

If you are developing a relatively small application, most probably you might opt to have an in memory database rather than having a SQL or Access or any other heavy weight database. So that you can develop and deploy your application without need of a database server. For this we can go for several options like plain text files, xml files etc.

Because of lots of benefits most of the times you will go for a xml database. In previous versions of .Net it was bit difficult to access and manipulate the those xml databases. But with the introduction of LINQ (with .Net framework 3.5) accessing and manipulation became very easier than it was before. In this article I wont go for much details about LINQ and its benefits and other details in very deep since there are lots of documentation in the web. So here I will just give some samples for some of the main data access and manipulation operations.

Consider the following XML file, 


01) Select operation

        private void GetUsers()
        {
            label1.Text = string.Empty;
            XDocument xmlDB = XDocument.Load("TestDB.xml");

            var users = from user in xmlDB.Descendants("User")
                        select new
                        {
                            UserID = user.Attribute("id").Value,
                            FirstName = user.Element("FirstName").Value,
                            LastName = user.Element("LastName").Value,
                        };

            foreach (var user in users)
            {   //Here im just writing the out put in to a labal. You can do what ever you want as appropriately.
                label1.Text += user.UserID + "\n" + user.FirstName + "\n" + user.LastName + "\n\n";
            }
        }       

02) Insert New User

        private void AddUser()
        {
            XDocument xmlDoc = XDocument.Load("TestDB.xml");          
            int userID = 0;
            userID = xmlDoc.Descendants("User").Max(id => (int)id.Attribute("id"));
            userID += 1;        

            xmlDoc.Root.Element("Users").Add(
                new XElement("User",
                       new XElement("FirstName", "Wijaya"),
                       new XElement("LastName", "Wijenayake"),
                       new XAttribute("id",userID.ToString())
                ));
            xmlDoc.Save("TestDB.xml");
        }

03) Update existing user

        private void UpdateUser(int userID)
        {
            XDocument xmlDoc = XDocument.Load("TestDB.xml");
            XElement user = xmlDoc.Root.Element("Users").Elements("User").Where(u => (int)u.Attribute("id")                                                                                                          == userID).FirstOrDefault();

            if (user != null)
            {
                user.SetElementValue("FirstName", "Viraj");
                xmlDoc.Save("TestDB.xml");
            }          
        }

04) Delete existing user

        private void DeleteUser(int userID)
        {
            XDocument xmlDoc = XDocument.Load("TestDB.xml");
            XElement user = xmlDoc.Root.Element("Users").Elements("User").Where(u =>                                (int)u.Attribute("id") == userID).FirstOrDefault();

            if (user != null)
            {
                user.Remove();
                xmlDoc.Save("TestDB.xml");
            }          
        }




Wednesday, March 9, 2011

Accessing Infragistics Controls in Javascript

Accessing and editing infragistics controls in java scripts might have given you a hard time since you can't access them same as you access html or asp.net controls. But if you have looked at the documentation of infragistics controls, you know this is easier than accessing and editing (values) html controls. Look at the following sample code.

HTML markup:







Javacript function:

function checkValue()
{
      var tbxPrice=  igedit_getById(<%= "'" + tbxPrice.ClientID + "'" %>);
      if (tbxPrice != null && tbxPrice.getValue() > 100)
     {
          tbxPrice.setValue(100);
     }
}

These are the Methods used which are given by infragistics libraries.

igedit_getById() - get the object reference of control
getValue()  - get the value
setValue()  - set the control
 

Tuesday, March 8, 2011

Monitor table locks in SQL server database

At any given time, tables in a database can be locked because of several reasons. Most probably this can be happen,
  • Not commiting a transaction after insert or update query
  • Not roll backing a transactions after encounting  exceptions
When there are table locks in the DB, these tables can not be accessed from any other transaction. So that the query execution on these tables will be blocked. Hence some functionalities in your system might be stuck until you restart database or your application.
In your system you might experience some functionalities are been stuck only in some times. Since it is happening randomly it can not be a problem in your code. And its really hard to figure out them. There is a possibility that this is happening because of table locks. To make sure that, we have to identify which tables are been locked at a given time. Following queries are help you for this.


--Querry for display locked tables in the given database
select db.name as DBName,
    db.dbid as DBID,
    sysobjects.name as 'LockedObjects',
    locks.request_Type,
    locks.Request_status,
    locks.request_owner_type
from sys.dm_tran_locks as locks
inner join sysobjects on sysobjects.id = locks.resource_associated_entity_id
inner join master..sysdatabases db on db.dbid = locks.resource_database_id
where db.name = 'corpdb' --Put you database name here

--Querry for display blocked uses calls in the given database
select cmd as command,
    process.waittime,
    process.hostname,
    process.program_name,
    process.loginame as LoginName,
    process.status
from sys.sysprocesses process
inner join master..sysdatabases db on db.dbid = process.dbid
where blocked > 0
and db.name = 'corpdb' --Put you database name here