Sunday, June 29, 2008

[ASP.NET] 讀取RSS前,先設定Proxy

最近,因為要做的新聞發佈平台,時間關係,想偷懶直接使用現成的Blog,然後透過RSS讀取最新的訊息就好,哪知,提供伺服器的環境,竟然需要設Proxy才連得出去,只好去survey一下,要怎麼設Proxy囉~

System.Net.WebRequest req = System.Net.WebRequest.Create(strRssUrl);
req.Proxy = new System.Net.WebProxy(yourproxyserver, portnumber);
req.Proxy.Credentials = CredentialCache.DefaultCredentials;
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader textReader = new System.IO.StreamReader(resp.GetResponseStream());
XmlTextReader xmlReader = new XmlTextReader(textReader);
XmlDocument xmlDoc= new XmlDocument();
xmlDoc.Load(xmlReader);
XmlNodeList rssList = xmlDoc.GetElementsByTagName("item");

//把最新訊息的內容輸出到網頁上
labelLatestNews.Text = rssList[0].ChildNodes[5].InnerText;
//留下詳閱全文的hyperlink
linkLatestNews.NavigateUrl = rssList[0].ChildNodes[6].InnerText;

Labels:

Tuesday, June 10, 2008

[ASP.NET] 防止XSS與SQL Injection

最近在研究這方面的困擾,網路上找到不錯的文章,避免下次要用又找不到,先把它存下來。

How To: Prevent Cross-Site Scripting in ASP.NET
http://msdn.microsoft.com/en-us/library/ms998274.aspx

How To: Protect From SQL Injection in ASP.NET
http://msdn.microsoft.com/en-us/library/ms998271.aspx

Labels:

Wednesday, June 04, 2008

[ASP.NET] 攫取新增到資料庫最新的Identity或autonumber的值

搞了好久,總算找到比較完整的解決方式,
問題是在當我們新增一比紀錄到資料庫的時候,
往往,我們需要知道這個新紀錄的某個值,而這個值碰巧是SQL資料庫自己新增的,
那,我們要怎麼辦到不需要重新再Query一次,
就可以把值取出來呢?

MSDN上有提供完整的教學,看看吧~
http://msdn.microsoft.com/zh-tw/library/ks9f57t0.aspx

Labels:

Monday, May 26, 2008

[SQL]安裝SQL 2005出現 『Increment the Counter Registry Key for Setup in SQL Server 2005 效能監視器計數器需求』錯誤

原因發生在之前安裝過SQL 2005 Express,後來被我移除之後,改裝SQL 2005 Enterprise,卻發生這樣的錯誤訊息,上網找了一下,原來是微軟的問題,而且,很多人都發生過。
解決方式很簡單:

  1. 依序按一下「開始」和「執行」,在「開啟」中輸入 regedit,然後按一下「確定」按鈕。
  2. 在「登錄編輯器」,找到下列機碼:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib
    您會發現如下這兩個機碼名稱與機碼值(您的機碼值可能跟我的不一樣,那沒有關係):
    Last Counter=00001dd2 (7634)
    LastHelp=00001dd3 (7635)
  3. 接著將展開「009」該節點,在右邊窗格中的 Counter 機碼上,用滑鼠連按兩次來察看其最大值是多少,將捲軸拉到最下面,假設,最大值為 7635。
  4. 回到步驟 2 中,確認 Last Help 值 (7635) 必須符合上個步驟 Perflib\009 中的 Help 機碼的最大值 7635。
  5. 若不符何時,請修改步驟 2 中的 Last Counter 值和 Last Help 值,修改方式如下:
    以滑鼠右鍵按一下右邊窗格中的 Last Help ,連按兩下滑鼠左鍵,接著按一下 底數 下面的 ○ 十進位(D),在 數值資料 文字方塊中,鍵入設定值 7635 (此數值必須與Perflib\009中的Help機碼的最大值相符),然後按一下 [確定]。
    以滑鼠右鍵按一下右邊窗格中的 Last Counter ,連按兩下滑鼠左鍵,接著按一下 底數 下面的 ○ 十進位(D),在 數值資料 文字方塊中,鍵入設定值 7634 (此數值必須與Perflib\009中的Counter機碼的最大值相符),然後按一下 [確定]。
    關閉 [登錄編輯器]。
  6. 重新執行 SQL Server 安裝程式。

中文版的系統,請參考Perflib\004中的數值。

微軟中文解說 http://forums.microsoft.com/MSDN-CHT/ShowPost.aspx?PostID=679903&SiteID=14
微軟英文解說 http://msdn.microsoft.com/en-us/library/ms143215.aspx

Labels:

Friday, May 09, 2008

[.NET] 無法存取 IIS Metabase

今天開始寫自己的第一隻ASP.NET網頁,裝好IIS、Visual Web Developer 2008 Express.
在IIS建立了虛擬路徑,指到自己的工作目錄,執行http://localhost/istocktravel/default.aspx
竟然給我出現了『無法存取 IIS Metabase』。
後來上網查了一下,竟然輕鬆的讓我查到solution,而且一試就靈,
1. 開始 > 執行 > cmd
2. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -i


重新註冊ASP.NET就好了,奇怪,這應該是個bug吧,微軟沒有QA嗎?

Thursday, December 28, 2006

[Ajax] To abort querying server in some conditions

In some conditions, you will need to abort Ajax to query server immediately.
XMLHttpRequest object provides a function, abort(), to let you accomplish this goal.
The syntax is as following:

//myAjax is a XMLHttpRequest object
myAjax.abort();

However, you might use some 3rd party script to enhance the convenience of constructing Ajax request. For example, we used popular Prototype.js created by ( Sam Stephenson - web site: http://prototype.conio.net/) to approach it.

Prototype.js provides a method to return the XMLHttpRequest object.

var myAjax = new Ajax.Request(url, {method:'post', postBody:PostArgument , onComplete:ResponseFunctionName});
AjaxGetServerMore = myAjax.transport;

In some conditions that you define, you can use abort() method to abort querying server, such as setting time-out period.

AjaxGetServerMore.abort();

[Ajax] Ajax IE Caching Issue

If you have a task that needs to periodically query server through Ajax, you might suffer from cache in IE. The solution resolving this problem is quite easy.
It is to add ‘?random=’ + Math.random(); to the end of all our requests.

Thursday, November 30, 2006

[Script] How to detect client's browser type and version (only support IE, Firefox & Netscape)?

I hypothesize you want to recognize client's type and version. To approach this goal, you should take advantage of navigator object.

The navigator object has the following properties:

  1. appCodeName
    The code name of the browser.
  2. appName
    The name of the browser (ie: Microsoft Internet Explorer).
  3. appVersion
    Version information for the browser (ie: 4.75 [en] (Win98; U)).
  4. cookieEnabled
    Boolean that indicates whether the browser has cookies enabled. IE4 and NS6+.
  5. language
    Returns the default language of the browser version (ie: en-US). NS4 and NS6+ only.
  6. mimeTypes[]
    An array of all MIME types supported by the client. NS4 and NS6+ only. Array is always empty in IE.
  7. platform[]
    The platform of the client's computer (ie: Win32).
  8. plugins
    An array of all plug-ins currently installed on the client. NS4 and NS6+ only. Array is always empty in IE.
  9. systemLanguage
    IE4+ property that returns the default language of the operating system. Similar to NS's language property.
  10. userAgent
    String passed by browser as user-agent header. (ie: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1))
  11. userLanguage
    IE4+ property that returns the preferred language setting of the user. Similar to NS's language property.
Practice 1 - Detect client's browser type:

var typeBrowser;
if(navigator.userAgent.indexOf("MSIE")>-1)
typeBrowser = "MS Internet Explorer";
else if(navigator.userAgent.indexOf("Firefox")>-1)
typeBrowser = "FireFox";
else if(navigator.userAgent.indexOf("Netscape")>-1)
typeBrower = "Netscape";
alert("Your browser is " +
typeBrowser);

Practice 2 - Detect client's brower version:

var BVersion,temp;
if(navigator.userAgent.indexOf("MSIE")>-1){
temp =
navigator.appVersion.split("MSIE");
//because the value of navigator.userAgent in IE is like: 4.0 (compatible;
MSIE 5.5; Windows 98; Hotbar 3.0)
BVersion = parseFloat(temp[1]);
}
else if(navigator.userAgent.indexOf("Firefox")>-1){
temp =
navigator.appVersion.split("Firefox/");
//because the value of navigator.userAgent in IE is like: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4
BVersion =
parseFloat(temp[1]);
}
else if
(navigator.userAgent.indexOf("Netscape")>-1){
temp = navigator.appVersion.split("Netscape/");
//because the value of navigator.userAgent in IE is like: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)

BVersion = parseFloat(temp[1]);
}
alert("The version of your browser is " + BVersion + " or above");