Monday, January 30, 2012

How to iterate through a DataTable in C#?

It often happens when you have a datatable in c# but you don't know how many columns has and its names. So, please take a look at this simple example to loop over a datatable rows. If you have any question don't hesitate in comments section.
DataTable dtCompanies = GetCompanies();
        string Message = "";
        int rowCount = 0;
        foreach (DataRow dRow in dtCompanies.Rows)
        {
            Message += "Row:" + rowCount + ", Columns: ";

            foreach (DataColumn dCol in dtCompanies.Columns)
            {
                Message += dCol.ColumnName + "( " + dtCompanies.Rows[rowCount][dCol].ToString() + " ) ";
            }
            rowCount++;
        }

Saturday, January 21, 2012

How to shut down an application in Asp.Net?

Often when you are updating your website you would not be able to overwrite files, operate in database. That is why you can do the following. Create a file called app_offline.html. In this file you can insert content to show as long time as your application is down for maintenance. You can look here form my application offline page:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Application Offline</title>
    <style>
        p
        {
            background-color: #ffffcc;
            padding-top: 10px;
            padding-bottom: 10px;
            padding-left: 10px;
            padding-right: 10px;
            border-style: solid;
            border-color: Black;
            border-width: 1px;
        }
    </style>
</head>
<body>
    <h1 class="error">
        Website is updating
    </h1>
    <p style="font-size: 15px;">
        This site is currently updating. Please wait for a while.
        <br />
        Thanks.
    </p>
</body>
</html>



The way app_offline.htm works is that you place this file in the root of your application. When ASP.NET sees it, it will shut-down the app-domain for the application (and not restart it for requests) and instead send back the contents of the app_offline.htm file in response to all new dynamic requests for the application. When you are done updating the site, just delete the file and it will come back online or rename to _app_offline(for example) and you will have the file there for use next time you need it.

Also have in mind that by adding the app_offline.htm the application sends the Application_End and after this function return the rest of the threads of the program are killed. The maximum time of wait for theApplication_End to return is set on the pool settings.

If you stop the full pool then all the sites that under this pool follow the same procedure. If you only open the app_offline.htm then only this site is affected.

To avoid your threads to kill by this shutdown, set a wait state on the Application_End
void Application_End(object sender, EventArgs e) 
{
    // This is a custom function that you must make and
    //   check your threads in the program
    MyTheadClass.WaitForAllMyThreadsToExist();

    // after this function exit the rest of the threads are killed.
}

If you use the app_offline.htm feature, you should make sure you have at least 512 bytes of content within it to make sure that your HTML (instead of IE's friendly status message) shows up to your users.  If you don't want to have a lot of text show-up on the page, one trick you can use is to just add an html client-side comment with some extra content to push it over 512 bytes. You can insert html comments just to make your file bigger.

Thursday, January 19, 2012

Html web color codes and chart, how to get hexadecimal values

It happens very often when you need to get hexadecimal values for colors so you can set them as background color or color of a certain text in CSS style without opening Photoshop or other software. So I think that a useful link is this one http://html-color-codes.info/. I wrote this link because it was useful to me. It is not because of advertising reasons. :)

Tuesday, January 17, 2012

How to detect browser name and version in JavaScript?

In case that you wonder how to detect version of current browser in javascript you can use the next code. If you have any question please leave a comment.
var navigatorVersion = navigator.appVersion;
var navigatorAgent = navigator.userAgent;
var browserName = navigator.appName;
var fullVersionName = '' + parseFloat(navigator.appVersion);
var majorVersionName = parseInt(navigator.appVersion, 10);
var nameOffset, verOffset, ix;

// In Firefox, the true version is after "Firefox" 
if ((verOffset = navigatorAgent.indexOf("Firefox")) != -1) {
    browserName = "Firefox";
    fullVersionName = navigatorAgent.substring(verOffset + 8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset = navigatorAgent.indexOf("MSIE")) != -1) {
    browserName = "Microsoft Internet Explorer";
    fullVersionName = navigatorAgent.substring(verOffset + 5);
}

// In Chrome, the true version is after "Chrome" 
else if ((verOffset = navigatorAgent.indexOf("Chrome")) != -1) {
    browserName = "Chrome";
    fullVersionName = navigatorAgent.substring(verOffset + 7);
}

// In Opera, the true version is after "Opera" or after "Version"
else if ((verOffset = navigatorAgent.indexOf("Opera")) != -1) {
    browserName = "Opera";
    fullVersionName = navigatorAgent.substring(verOffset + 6);
    if ((verOffset = navigatorAgent.indexOf("Version")) != -1)
        fullVersionName = navigatorAgent.substring(verOffset + 8);
}

// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset = navigatorAgent.indexOf("Safari")) != -1) {
    browserName = "Safari";
    fullVersionName = navigatorAgent.substring(verOffset + 7);
    if ((verOffset = navigatorAgent.indexOf("Version")) != -1)
        fullVersionName = navigatorAgent.substring(verOffset + 8);
}

// In most other browsers, "name/version" is at the end of userAgent 
else if ((nameOffset = navigatorAgent.lastIndexOf(' ') + 1) <
          (verOffset = navigatorAgent.lastIndexOf('/'))) {
    browserName = navigatorAgent.substring(nameOffset, verOffset);
    fullVersionName = navigatorAgent.substring(verOffset + 1);
    if (browserName.toLowerCase() == browserName.toUpperCase()) {
        browserName = navigator.appName;
    }
}
// trim the fullVersionName string at semicolon/space if present
if ((ix = fullVersionName.indexOf(";")) != -1)
    fullVersionName = fullVersionName.substring(0, ix);
if ((ix = fullVersionName.indexOf(" ")) != -1)
    fullVersionName = fullVersionName.substring(0, ix);

majorVersionName = parseInt('' + fullVersionName, 10);
if (isNaN(majorVersionName)) {
    fullVersionName = '' + parseFloat(navigator.appVersion);
    majorVersionName = parseInt(navigator.appVersion, 10);
}

//document.write(''
// + 'Browser name  = ' + browserName + '
'
// + 'Full version  = ' + fullVersionName + '
'
// + 'Major version = ' + majorVersionName + '
'
// + 'navigator.appName = ' + navigator.appName + '
'
// + 'navigator.userAgent = ' + navigator.userAgent + '
'
//)

My most popular images for sale at Shutterstock:

Find my story and how you can get into stock photography, too.

Monday, January 16, 2012

How to fix permissions for files on a server?

Often after you upload files on a server you will get to the point when you won't be able to edit/delete add other files. It happened to me when I unzipped an archive directly on server with a php file.
So how do we change attributes?


set_time_limit ( 0 );
ini_set("memory_limit","1000M");

file_fix_directory(dirname(__FILE__));

function file_fix_directory($dir, $nomask = array('.', '..', 'CVS')) {
  if (is_dir($dir)) {
     // Try to make each directory world writable.
     if (@chmod($dir, 0777)) {
       echo "

Made writable: " . $dir . "

"; } } if (is_dir($dir) && $handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if (!in_array($file, $nomask) && $file[0] != '.') { if (is_dir("$dir/$file")) { // Recurse into subdirectories file_fix_directory("$dir/$file", $nomask); } else { $filename = "$dir/$file"; // Try to make each file world writable. if (@chmod($filename, 0777)) { echo "

Made writable: " . $filename . "

"; } } } } closedir($handle); } }

Monday, January 9, 2012

How to substract 2 ArrayLists in C#?

Well the answer might be really easy for some of you but for newbies is not.
So considering that we have 2 arraylists.
 
ArrayList FirstArray = new ArrayList(new[] { "23", "25", "27" });
ArrayList SecondArray = new ArrayList(new[] { "25", "28", "29" });


and we want the difference.For that this is what you have to do. Call a method that I implemented.
 
ArrayList Difference = Substract2ArrayLists(FirstArray, SecondArray, true);
Method:
 
    /// 
    /// Substracts 2 array lists/a difference
    /// 
    /// 

First ArrayList
    /// 

Second ArrayList
    /// 

True if compare string, False for long values
    /// 
    static public ArrayList Substract2ArrayLists(ArrayList First, ArrayList Second, bool IsString)
    {
        if (IsString)
        {
            foreach (string secondValue in Second)
            {
                First.Remove(secondValue);
            }
        }
        else
        {
            foreach (long secondValue in Second)
            {
                First.Remove(secondValue);
            }
        }
        return First;
    }

Monday, January 2, 2012

How to insert embedded code into a post on Blogger?

As I have just started to write in this blog 3 days ago I came to an issue: how to insert highlighted code, snippets of code: csharp, javascript, css, sql, cpp, python, ruby, xml or perl. I googled a little and found this solution: Go to Dashboard page of your blog and go to Template -> Edit HTML -> Proceed. Then just before closing of head tag, insert next lines.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

As you can see source of codes come from http://alexgorbatchev.com/SyntaxHighlighter/ so you can find last updates right on that blog. Of course, if you are not going to insert let's say Python code than it won't be necessary no insert that line () Save your template and go to a new post. There are two possibilities to insert snippets code.

  1. Using script tag
  2. 
    
    Result is:
  3. Using the pre tag
  4. // Comment
    public class Test2 {
    public Test2() {
    }
     
    public void AMethod() {
    /* Another comment */
    string  var = "Message";
    }
    }
    
    Result is:
    // Comment
    public class Test2 {
    public Test2() {
    }
     
    public void AMethod() {
    /* Another comment */
    string  var = "Message";
    }
    }