2011年5月5日 星期四

找出資料庫中 起訖時間 overlap的 record

WHERE

開始時間 <= $off_time
AND
結束時間 >= $on_time

圖解,以下 case 都要找出來

--------------------------------------------------------------------------------
                  $on_time                         $off_time
                        |                                        | 
                        |                                        |
                        |                                        |
              start-------- end
                        start------------------------ end
                                  start--------- end
     start-------------------------------------------------------- end
                                            start------------------------ end

2011年4月28日 星期四

GeoLocation sample code

var initialLocation = new Object();
    initialLocation.type = "CHT_Xinyi";
    initialLocation.x = "121.52435034130934";
    initialLocation.y = "25.03645913419541";
    initialLocation.h = "0";
    initialLocation.cityName ="";
    initialLocation.countryCode="";
    initialLocation.countryName = "TAIWAN";
   
function getUserGeoLocation(){
   
    var browserSupportFlag =  new Boolean(false);
    // Try W3C geolocation
    if(navigator.geolocation) {
        browserSupportFlag = true;
        //successCallback
        navigator.geolocation.getCurrentPosition(function(position) {
          
            initialLocation.type = "Location found using W3C standard";
            initialLocation.x = position.coords.longitude;
            initialLocation.y = position.coords.latitude;
            initialLocation.h = position.coords.altitude;;
            $("#type").html(initialLocation.type);
            $("#x").html(initialLocation.x);
            $("#y").html(initialLocation.y);
            $("#h").html(initialLocation.h);
            $("#cityName").html(initialLocation.cityName);
            $("#countryCode").html(initialLocation.countryCode);
                      
        //errorCallback
        }, function() {
            alert("navigator.geolocation exist...getCurrentPosition failed");          
        });
    // Try IP location IPInfoDB
    } else if (browserSupportFlag==false) {
   
        var service_url = "http://***********/geolocation/geolocation.aspx";
        $.ajax({
            url: service_url,
            type: 'GET',
            dataType: 'json',
           // async:false,
            error: function (xhr, textStatus) {
                alert(xhr.statusText);
            },
            success: function (data) {
                if (data == null) {
                    alert('NO RESULT');
                } else {
                    initialLocation.type = "Location found using IPInfoDB API";             
                    initialLocation.x = data.longitude
                    initialLocation.y = data.latitude;
                    initialLocation.h = "";
                    initialLocation.cityName = data.cityName;
                    initialLocation.countryCode = data.countryCode;
                    $("#type").html(initialLocation.type);
                    $("#x").html(initialLocation.x);
                    $("#y").html(initialLocation.y);
                    $("#h").html(initialLocation.h);
                    $("#cityName").html(initialLocation.cityName);
                    $("#countryCode").html(initialLocation.countryCode);
                }
            }
        });
    }
}

2011年4月11日 星期一

[ASP.NET] cookies C#

做完load balance之後,session會有問題....索性換成cookies

//set cookies
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["sn"] = "******";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);


//read cookies
string a = Request.Cookies["UserSettings"]["sn"];

//MSDN 嚴謹的作法
if (Request.Cookies["UserSettings"] != null)
{
    string userSettings;
    if (Request.Cookies["UserSettings"]["Font"] != null)
    { userSettings = Request.Cookies["UserSettings"]["Font"]; }
}

2011年4月5日 星期二

[iOS] acceleration data

面對手機的直立模式下 (portrait mode)
三軸 (three axes could be positive or negative)

X軸 horizontal across
y軸 vertical across
z軸 runs through from front to back

Two primary classes
  • UIAccelerometer
  • UIAcceleration
How to utilize UIAccelerometer
3 steps
  1. Get the shared accelerometer object
  2. Config the delivery interval (default 100 times/second)
  3. Set the delegate 
    • object to receive acceleration events
    • data is sent to the main thread repeatedly until removed.

How to utilize UIAcceleration
store the data associated with an acceleration event
read-only properties x, y, z

2011年3月31日 星期四

[ASP.NET] refresh 帶參數的網頁

// html
//this line puts between < / head > tag
< asp : literal   id=metaLiteral  runat=server>< / asp:litera l>

//c#
metaLiteral.Text = < meta http-equiv=refresh content=5;URL=availablePeriodById.aspx?ad= + ad + &year= + year + >

2011年3月26日 星期六

ASP.NET data binding

// data source: collections or database
// such as arraylist, array, set, database table, view.....

//ASP.NET 1.0
//controlName.DataSource = collectionName
//controlName.DataBind();

//advatage: no need to use for loop to insert element into html control

2011年3月25日 星期五

ASP.NET eventlog

例外紀錄的方式有:
  1. 寫到文字檔
  2. 寫到系統eventlog
  3. send mail
  4. 寫到資料庫

範例C#:將"例外"寫到windows eventlog

系統管理工具 事件檢視器

using system.diagnostics.eventlog

try
{

  placeOrder(1);

}
catch(Exception ex)
{
  EventLog log = new EventLog();
  log.Source = "進銷存系統";
  //log.WriteEntry(ex.Message);
  log.WriteEntry(ex.Message, EventLogEntryType.Error);
}

private void placeOrder(int id)
{
  if(id == 1)
  {
      throw new Exception("sold out!!");
  }
}