nLL Mobile web, .Net, Android, gadgets and some random stuff

simple mobile ajax

30. November 2010 18:57 by nolovelust in Mobile web, Useful

When designing a modern mobile site your obvious choice for ajax enabled site is jQuery. Although latest mobile phones are quite capable of javascript and ajax; unfortunatelly jQuery or jQuery Mobile does not work on many of them yet. I'm not talking about iPhone or Android phones but non-smart phones or feature phones without advanced operating sytems. You need simpler version of ajax scripts.

After some tests on my site I came up with below ajax functions that usually works on javascript enabled phones

 

var ajaxRequest;
var loading;
loading = new Image();
loading.src = "pathtoyourloadinganimation.gif";

function checkXMLHttpRequest() {
    if (window.XMLHttpRequest) {
        ajaxRequest = new XMLHttpRequest();
        return true;
    }
    else try {
        ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
        return true;
    } catch (e) {
        try {
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            return true;
        } catch (e) {
            ajaxRequest = false;
            return false;
        }
    }
}
function ajaxPost(mydiv, url, params) {
    if (checkXMLHttpRequest()) {
        ajaxRequest.open('POST', url, true);
        ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        ajaxRequest.setRequestHeader("Content-length", params.length);
        ajaxRequest.setRequestHeader("Connection", "close");
        ajaxRequest.onreadystatechange = function () {
            if (ajaxRequest.readyState == 4) {
                if (ajaxRequest.status == 200) {
                    document.getElementById(mydiv).innerHTML = ajaxRequest.responseText;
                }
                else {
                    document.getElementById(mydiv).innerHTML = 'Could not retrieve data';
                }
            }
            else {
                document.getElementById(mydiv).innerHTML = 'Loading...';

            }
        }
        ajaxRequest.send(params);
        return true;
    }
    else {
        return false;
    }
}

function ajaxGet(mydiv, url) {
    if (checkXMLHttpRequest()) {
        ajaxRequest.open('GET', url, true);
        ajaxRequest.setRequestHeader('Content-Type', 'text/html');
        ajaxRequest.onreadystatechange = function () {
            if (ajaxRequest.readyState == 4) {
                if (ajaxRequest.status == 200) {
                    document.getElementById(mydiv).innerHTML = ajaxRequest.responseText;
                }
                else {
                    document.getElementById(mydiv).innerHTML = 'Could not retrieve data';
                }
            }
            else {
                document.getElementById(mydiv).innerHTML = 'Loading...';

            }
        }
        ajaxRequest.send();
        return true;
    }
    else {
        return false;
    }
}

 

Ajax get sample

 

function getdemo(loadingdiv) {
    var url = '/get.aspx';
    if (!ajaxGet(loadingdiv, url)) {
        alert('Unable to load reviews!');
    }
}

 

Ajax post sample

 

function postdemo(param1, param2, loadingdiv) {
        var url = '/post.aspx';
        var params = 'param1=' + param1 + '&param2=' + param2;
        if (!ajaxPost(loadingdiv, url, params)) {
            alert('Unable to post your vote!');
        }
    }
}

 

Building asp.net web applications with sqlite

26. November 2010 13:43 by nolovelust in ASP.NET, Useful

If you want to build and asp.net web site/application that is interactive you mostly use SQL server as backend for your site.
But sometimes you don't need full-fledged server; you could be building a personal site or a site with low traffic. In cases where you don’t need a SQL server one of the choices is SQLite database.
I’ve build couple of sites with SQLite and quite happy with performance. Although it is not recommended to use it on high traffic sites I have not seen any problem with sites that have 50-60 users online at any given time.


To use SQLite on your site as database provider you need couple of things


• Up-to-date version of SQLite .Net from http://sqlite.phxsoftware.com/
• To create/design my databases I use SQLite Admin from http://sqliteadmin.orbmu2k.de/ but you can use Visual Studio addin of SQLite .Net too.

Once you install above files all you need to do is create an empty asp.net website/application and put System.Data.SQLite.DLL in to your Bin folder. Create a SQLite database and put it to your App_Data folder.
You can connect to it with connection string like below

          static string connString = "Data Source=" + SqliteDataBasePath + "; Journal Mode=Off; Compress=True; Version=3;";

I use below class to communicate with SQLite Databases. It is quite simple.

using System.Data;
using System.Data.SQLite;
using System.Web;

/// 
/// Summary description for DB
/// 
public class DB
{

    public static DataSet GetTables(string sql)
    {
        DataSet ds = new DataSet();

        using (SQLiteConnection conn = new SQLiteConnection(connString))
        {
            conn.Open();
            using (SQLiteCommand cmd = new SQLiteCommand(conn))
            {
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd))
                {
                   
                    da.Fill(ds);
                    return ds;
                }
            }
        }

    }
    public static int ExecuteNonQuery(string sql)
    {
        using (SQLiteConnection conn = new SQLiteConnection(connString))
        {
            conn.Open();
            using (SQLiteCommand cmd = new SQLiteCommand(conn))
            {
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                int rowsUpdated = cmd.ExecuteNonQuery();
                return rowsUpdated;

            }
        }

    }
    public static string ExecuteScalar(string sql)
    {

        using (SQLiteConnection conn = new SQLiteConnection(connString))
        {
            conn.Open();

            using (SQLiteCommand cmd = new SQLiteCommand(conn))
            {
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                object value = cmd.ExecuteScalar();
                if (value != null)
                {
                    return value.ToString();
                }
                else
                {
                    return "";
                }
            }

        }
    }  
    
}

Apple's iAd Mobile Advertising Network coming To UK. Guess How Much Is the Minimum Budget?

26. November 2010 12:46 by nolovelust in iPhone, mobile internet, Mobile web

According to source below, minimum ad budged for Appl'es iAd Platform is $1.000.000 in the U.S and £600.000 in the U.K. 

So unless you are a very big company and wan't to target Apple products only prices are way to high.

Source http://www.nma.co.uk/news/is-apple-pricing-itself-out-of-mobile-ads?/3020837.article via http://londoncalling.co/2010/11/will-apples-iad-fail-at-the-first-hurdle-in-the-uk/

Unable to disable Asp.Net 4 Request Validation Errors?

25. November 2010 11:50 by nolovelust in

Since I started to develop with .Net 4 something was bugging me. I was unable to disable  Request Validation like on 2. There is option on .Net 4 that you can set it .Net 2 model with

   requestValidationMode="2.0" 

So that you can enable/disable validation on page level. But, if you are using .Net 4 WebForms Routing sometimes you end up with errors like

   A potentially dangerous Request.Path value was detected from the client

Although it is good thing to filter out dangerous requests in my case i need to have them present in the url.

There is a simple solution for it. You just need to set HttpRuntimeSection.RequestPathInvalidCharacters Property as you like. Default illegal chars are  "<,>,*,%,&,:,\,?".

You can change them in your web.config. Here is my perefered settings

<httpRuntime requestValidationMode="2.0" requestPathInvalidCharacters="*,:" /> 

For more info read

HttpRuntimeSection.RequestValidationMode Property
HttpRuntimeSection.RequestPathInvalidCharacters Property
http://blog.tjitjing.com/index.php/2010/10/how-to-remove-characters-to-avoid-net-request-validation-error-a-potentially-dangerous-request-path-value-was-detected-from-the-client.html