Wednesday, October 10, 2012

How to get total number of tables in a database in SQL?

It might happen that you will have to start working on a big database, someday. So, just out of curiosity if you ever want to find total number of tables that are in your table you can use this simple query:


SELECT COUNT(*) as TotalTables FROM sys.tables WHERE type in ('u') 

  
Of course, first you have to connect to your database. Hope it helps.

Thursday, October 4, 2012

How to get country, city, language for an ip address, in Asp.Net?

When you want to get location information about a client that visits your website according to its ip address you can use a third party service that offers this info in real time or you can use free services, which might not work 100% accurate. However, no matter which service you shall choose you can use the same workflow as I used in my project example.

I created a class that will be used to deserialize json response from api service from easyjquery.com:
 private struct GeoIPResponse
    {
//this is the response I get
        //{"IP":"127.0.0.1","continentCode":"Unknown","continentName":"Unknown",
        //"countryCode2":"Unknown","COUNTRY":"Unknown","countryCode3":"Unknown","countryName":"Unknown","regionName":"Unknown",
        //"cityName":"Unknown","cityLatitude":0,"cityLongitude":0,"countryLatitude":0,"countryLongitude":0,"localTimeZone":"Unknown",
        //"localTime":"0"}
        public string IP;
        public string continentCode;
        public string continentName;
        public string countryCode2;
        public string COUNTRY;
        public string countryCode3;
        public string countryName;
        public string regionName;
        public string cityName;
        public string cityLatitude;
        public string cityLongitude;
        public string countryLatitude;
        public string countryLongitude;
        public string localTimeZone;
        public string localTime;
    }
/*
then I defined some needful methods: 
*/

/// 
    /// Returns Client Ip Address
    /// 
    static public string ClientIpAddress
    {
        get
        {
            string _clientIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (!string.IsNullOrEmpty(_clientIPAddress))
            {
                string[] ipRange = _clientIPAddress.Split(',');
                _clientIPAddress = ipRange[ipRange.Length - 1];
            }
            else
            {
                _clientIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            return _clientIPAddress;
        }

    }

    public string _WebRequest(string strURL)
    {
        String strResult;
        WebResponse objResponse;
        WebRequest objRequest = HttpWebRequest.Create(strURL);
        objRequest.Method = "GET";

        objResponse = objRequest.GetResponse();
        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
        {
            strResult = sr.ReadToEnd();
            sr.Close();
        }
        return strResult;

    }
and finally this is the way I used a service from www.easyjquery.com: :
protected void Page_Load(object sender, EventArgs e)
{
        txtMessage.Text = "";
        string Result = _WebRequest("http://api.easyjquery.com/ips/?ip=" + ClientIpAddress + "&full=true");

        txtMessage.Text = Result;
        
        JavaScriptSerializer jss = new JavaScriptSerializer();
        var clientGeoLocation = jss.Deserialize < GeoIPResponse >(Result);

        lblMessage.Text = "
Your short details: " + "Country: " + clientGeoLocation.COUNTRY + ", CountryCode 2: " + clientGeoLocation.countryCode2 + ", CountryCode 3: " + clientGeoLocation.countryCode3 +
            ", TimeZone: " + clientGeoLocation.localTimeZone;
}

Wednesday, October 3, 2012

How do you test if a string is null or empty?

Today as I was studying something related to Asp.Net I got to an example where I had to test if a string variable is null or just an empty string. So, which are the possibilities and which is the most efficient?

  • if(myString == String.Empty)
  • if(myString == "")
  • if(myString.Length == 0) - this could throw an exception if myString is null
  • I also found this comparison: if( String.Equals(myString, String.Empty) )
  • and of course method IsNullOrEmpty introduced in .Net Framework 2:
    if(string.IsNullOrEmpty(myString)) which is said to be the most efficient.
Now, which methods do you use when comes to test if a string has a value or not? You are invited to write them as comments :) Of course that no matter which one of the methods above you choose won't slow down to much the performance of your application, but is good to build a good practice for future projects, isn't it?

Tuesday, September 18, 2012

How to format a number to x decimal places in C#?

If you have a decimal, double or int data type number and you want to show the number with a certain decimal places then you can use ToString() method from System namespace.
Have a look at these examples. I hope you will find what you need.

Friday, July 27, 2012

How to rename the /Umbraco directory?

When you want to rename the /Umbraco directory are two possibilities.

  1. You rename folder to 'admin', or whatever you want; change in web.config some lines - where is umbraco -> admin. Choosing this solution might not be that nice. When you will install a new package that will try to install on /Umbraco folder, so you will have to move files. Beside you still have to make some updates in Css files as it is hard-coded the paths.
  2. The second solution it is a very simple one :) Go to yoursite/Umbraco/UrlRewriting.config and add this  line in <rewrites> 




Good luck, ;)

Monday, July 23, 2012

How to create a sitemap/navigation for website in Umbraco with XSLT?

Working on a project, made in Umbraco CMS, I got to the point where I had to generate a navigation in footer. Just something like this






This is content Tree. Not all pages from footer image above appear in sitemap structure. I put the image just for you to realize what I had to do and what you might also want to achieve.

Now: according to my content, I wanted a list like this:

  •  About Patient Direct with childs bellow
  1.  Program Coverage (this page as you can see in image above has Hide in navigation = yes, so for me means they are childs of About....) 
  2.  Eligibility 
  •  Network 
  1.  About the Patient...
  2.  Dentist Search 
  •  Enroll Now 
  •  Wellness
Pretty nice, hm?
Good so how do you generate such a menu using Xslt?
Here is code I did and helped me.

< ? xml version="1.0" encoding="UTF-8" ? >
< ! DOCTYPE 
xsl:stylesheet [
    < ! ENTITY nbsp " ">
] >



    
    
    
    
    
        
            
        
    

    
        
        
        

            
                

        

    

    
        
        
  • bottom_border
  • bottom_border
If you need explanations about code please write it as a comment ;)

Monday, July 16, 2012

How to delete all tables from a Database?

If you want to delete all tables from a database you can help yourself by using this query:

DECLARE @TableName varchar(500)
DECLARE cur CURSOR
      FOR SELECT [name] FROM sys.tables WHERE type in ('u') 

      OPEN cur

      FETCH NEXT FROM cur INTO @TableName
      WHILE @@fetch_status = 0
      BEGIN
            EXEC('DROP TABLE ' + @TableName)
            --select @TableName
            FETCH NEXT FROM cur INTO @TableName
      END
      CLOSE cur
      DEALLOCATE cur 

     
In case you have constraints take special care you delete tables one by one ;)