2014年8月6日 星期三

C# WebRequest Error 500

暫時紀錄一下
             StringBuilder sb = new StringBuilder();
            string data = "";
            try
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(@"http://alerts.ncdr.nat.gov.tw/");
                myHttpWebRequest.Method = "GET";
                using (WebResponse myWebResponse = myHttpWebRequest.GetResponse())
                {
                    using (Stream myStream = myWebResponse.GetResponseStream())
                    {
                        using (StreamReader myStreamReader = new StreamReader(myStream))
                        {
                            data = myStreamReader.ReadToEnd();
                            richTextBox1.Text = data;
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                richTextBox1.Text = new StreamReader(ex.Response.GetResponseStream())
                                   .ReadToEnd();
            }
剛好案子上有使用到偵測網站是否是正常,但是很怪跑到一段IIS就跑出Error 500的問題,查詢了一下有可能是我使用了偵測是不是平板的Regex產生的問題,因為他有使用到 Request.ServerVariables["HTTP_USER_AGENT"]每次偵測到跑到這段就掛了先暫時紀錄一下之後再來處理

2014年8月4日 星期一

2014年8月3日 星期日

致冷片冷氣DIY

其他文章暫緩了,真的是熱到爆,編寫程式邊流汗讓我真的是沒辦法專心寫程式,
索性就這兩天就找了一些相關的資料,
準備來自己做一台冷氣 


準備的材料如下:
製冷片  TEC1-12706*5
PC用Power:350W*1
電線*1捲
散熱鋁塊*3
可變電阻300K-2*2
5W沉水馬達*1
透明食物保鮮盒*1
塑膠地墊*6
水管
鱷魚夾*10
臉盆*1







這些都是已經先處理過了,最終完成品如下











架構圖如下:

手繪線是水管
粉紅色的是水冷鋁塊,製冷片還有散熱鰭片
在水冷鋁塊下面有一個塑膠地墊作墊高,主要是讓散熱鰭片產生的冷凝水流到下面去
透明保鮮盒下面還有挖一個孔讓冷凝水流回到水桶裡
這整個Diy沒有去考慮到風扇的風量,還有冷房效率,散熱鰭片的對流,完完全全就按照想法下去弄。
而且也沒有溫度計去量測出風口的溫度,還有水桶裡的溫度,完全是用感覺去測試,
再出風口用手去感覺有沒有覺得冷,當風扇風速最高的時候,體感的感覺就像冷氣機26度左右,我在風扇的連接加上了一個可變電阻,去改變風扇的轉速,當風扇轉速越小,感受到的風就越冷
但是水桶裡的溫度,到是提升的非常的快,大約20分鐘那20公升的臉盆水就感覺標到了45度左右,1顆350W的Power 發出的熱量還真恐怖。
整體下來,我這次是用5片致冷片去做,我的客廳大約7坪,吹了20分鐘,完全沒感覺,整個大失敗,原因是出的風不夠冷,水桶內的水溫很快就會飆到很高,如果要開個一小時以上,真的就要再補上一個冷排把熱端產出來的熱水冷卻下來,熱端真是一個蠻大的問題﹒。
那如果真的要來當冷氣,其實也不是不可能,最大的問題就是要再增加致冷片,還有熱端的冷卻問題,致冷片感覺上需要10片以上,自己DIY的作法,如果要讓出的風夠冷,也許可以使用下面這種冷端的散熱鰭片讓空氣快速冷卻。
電源的部分,就要3顆POWER來供電,第一次測試的時候把所有致冷片都接到一條線上,當電源一開,龐大的電流瞬間就燒掉我一顆可變電阻............,之後學聰明了每一條電源接兩顆致冷片剛剛好,不過POWER的電線還是非常的熱,但是當你用到3顆350W的POWER,用電量就要再算過了,不過還是比傳統的冷氣還省電,改天再來用10顆~15顆來測試看看囉。
測試的時候老爹剛好在旁邊,一看到老爹一臉疑惑的臉神時,還有兒子充滿好奇的眼神,突然想到,那熱端產出來的熱,是不是可以在浴室上面接個小水塔用來存熱水,可以吹冷氣,又可以順便幫你把今天的熱水都備好,想喝熱開水隨時都有熱開水,不知道是不是可行,改天有空再來測試一下囉。
以後如果還有時間再來繼續致冷片冷氣DIY PART II

2014年7月27日 星期日

C# Asp.net FIleUpload event listen


jQuery("input#<%=FileUpload1.ClientID %>").change(function () {
                alert(jQuery(this).val());
            });

2014年7月17日 星期四

JavaScript 敲打文字時,驗證整數和小數



// 驗證小數
// doc:Doc物件
function ValiFloat(doc) {
    var docValue = $(doc).val();

    // 驗證小數pattern
    var valiPattern1 = /^\d+[.]?\d*$/;
    var reValiPattern1 = /^\d+[.]?\d*/;

    // 驗證數字格式pattern ,針對 01 replace 1
    var valiPattern2 = /^[0]+\d+/;
    var reValiPattern2 = /[1-9]\.?\d*/;

    if (!valiPattern1.test(docValue)) {
        $(doc).val(reValiPattern1.exec(docValue))
    }
    else if (valiPattern2.test(docValue)) {
        $(doc).val(reValiPattern2.exec(docValue))
    }

}

// 驗證整數
// doc:Doc物件
function ValiNumber(doc) {

    var docValue = $(doc).val();

    // 驗證整數pattern
    var valiPattern1 = /[^0-9]/g;

    // 驗證數字格式pattern ,針對 01 replace 1
    var valiPattern2 = /^[0]+\d+/;
    var reValiPattern2 = /[1-9]\.?\d*/;

    if (doc.value != doc.value.replace(valiPattern1, '')) {
        doc.value = doc.value.replace(valiPattern1, '');
    }

    else if (valiPattern2.test(docValue)) {
        $(doc).val(reValiPattern2.exec(docValue))
    }

}

// 使用方法就在Jquery 的 $(document).ready(function ()
$(document).ready(function () {
   // 驗證小數
   $(document).on("keyup","selector",function(){
        ValiFloat(this);
   })

   // 驗證整數
   $(document).on("keyup","selector",function(){     
        ValiNumber(this);
   })
})


2014年7月10日 星期四

MsSql DECLARE CURSOR 資料指標

Set Nocount ON;
declare  @a nvarchar(100),@b nvarchar(100),@c int
Declare db Cursor For select a,b  from  Test  order by a 
Open db
Fetch Next From f into @a, @b
While @@Fetch_Status = 0
Begin
   //Do Something
  fetch next from db into @a, @b
End 
Close db;
Deallocate db;

2014年7月7日 星期一

C 第一個程式 Hello World

其實我還蠻喜歡C的,在還沒開始寫C++跟C#的時候最早接觸就是C,在我的書櫃裡有一本最老的程式設計書 2003年出版的C語言入門的學習繪本,那時候看當然什麼都看不懂

 C介紹就不多講了,Google上有很多C的歷史自己去找 建置環境 Dev-C++或是VS都可以

 http://wenku.baidu.com/view/aa0dea7831b765ce05081418.html http://mitblog.pixnet.net/blog/post/37451428-visual-studio-2010-%E5%AF%ABc%E3%80%81visual-studio-2010-%E5%AF%ABc%E8%AA%9E%E8%A8%80%E3%80%81vi http://debugmode.net/2012/02/06/how-to-write-and-run-a-c-program-in-visual-studio-2010/


#include 
#include 

void main(void)
{
 printf("Hello World");
 system("pause");
}

C# Delete Process

如果您的WinAP執行了一大堆,開啟工作管理員一個一個刪除的話
那可以服用這段程式碼幫你快速刪除


foreach (Process p in Process.GetProcessesByName("process_Name")) //不需要.exe
{
   p.Kill();
}

真是輕鬆又快樂

Javascript KeyDown Listener

javascript版 鍵盤按鈕Listener 程式碼的數量真是..... 相較winap真是差太多
 window.addEventListener("keydown", checkKeyPressed, false);

 function checkKeyPressed(e) {
     alert(e.keyCode);
     if (e.keyCode == "65") {
         alert("The 'a' key is pressed.");
     }
 }


不過GridView分頁要用鍵盤數字鍵去做分頁選擇的話
比較簡單的做法,直接放一個TextBox和一個Button
後端Page_Load裡面一個 TextBox1.Focus();

前端
<div style="position: absolute; top: -1500px">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
 </div>
後端
 protected void Page_Load(object sender, EventArgs e)
 {
            if (!IsPostBack)
            {
           
            }
         
            TextBox1.Focus();
}

要注意Button2放的位置,如果上一個有Button的話,在TextBox按下ENTER 只會執行第一個Button

2014年7月6日 星期日

C# NPOI export xls

Third Part NPOI Nuget搜尋NPOI
 var workbook = new HSSFWorkbook();
 var sheetReportResult = workbook.CreateSheet("結果");
 
 sheetReportResult.CreateRow(0).CreateCell(0).SetCellValue("開始時間");
 
 //之後的用GetRow 取得在CreateCell
 sheetReportResult.GetRow(0).CreateCell(1).SetCellValue("結束時間");
 sheetReportResult.GetRow(0).CreateCell(2).SetCellValue("資料大小(KB)");
 
 sheetReportResult.GetRow(0).CreateCell(3).SetCellValue("花費時間(s)");
 
 sheetReportResult.GetRow(0).CreateCell(4).SetCellValue("速度(KB/s)");

 int i = 1;
 foreach (string str in sb.ToString().Split('|'))
 {
 
     if (str != "")
     {
         sheetReportResult.CreateRow(i).CreateCell(0).SetCellValue(str.Split(',')[0].Replace("Start Time: ", ""));
         sheetReportResult.GetRow(i).CreateCell(1).SetCellValue(str.Split(',')[1].Replace(" End Time :", ""));
         sheetReportResult.GetRow(i).CreateCell(2).SetCellValue(str.Split(',')[2].Replace(" Size:", ""));
         sheetReportResult.GetRow(i).CreateCell(3).SetCellValue(str.Split(',')[3].Replace(" Total:", ""));
         sheetReportResult.GetRow(i).CreateCell(4).SetCellValue(str.Split(',')[4].Replace(" Speed:", ""));
 
         i++;
     }
 }
 
 sheetReportResult.SetColumnWidth(0, 20 * 256);
 sheetReportResult.SetColumnWidth(1, 40 * 256);
 sheetReportResult.SetColumnWidth(2, 40 * 256);
 
 var file = new FileStream("C:/ample.xls", FileMode.Create);
 
 workbook.Write(file);
 file.Close();
 

2014年7月3日 星期四

JQuery Div Hide&Show

這段程式碼主要用在DIV顯示跟隱藏 剛好最近在幫客戶家功能,使用GridView做呈現 如果你非常懶的改後端,那就讓前端去幫你處理吧
$('#dvdeatil  div').each(function () {
 var data = $("#" + this.getAttribute('id')).attr("title");
 var county = data ? data.split(",")[0] : ""
   switch ($("#<%=ddlcounty.ClientID %>").find(":selected").text()) {
      case "全部":
         $("#tr_" + this.getAttribute('id')).show();
        break;
      default:
         $("#tr_" + this.getAttribute('id')).show();
         if (county != $("#<%=ddlcounty.ClientID %>").find(":selected").text())           {   
           $("#tr_" + this.getAttribute('id')).hide();
         }
       break;
    }
});

2014年7月2日 星期三

C# XmlDocument &XDocument Interconversion

using System.Xml.Linq;

XmlDocument  to  XDocument 

XmlDocument Xmldoc = new XmlDocument();
XDocument XDoc = XDocument.Parse(Xmldoc.OuterXml);

XDocument to XmlDocument

XDocument Xdoc = new XDocument();
XmlDocument Xmldoc = new XmlDocument();
Xmldoc.LoadXml(Xdoc.Document.ToString());

C# XML To List


最近經常跟Xml當好朋友,要是他能夠變成List<class>那就更好了,下面飯粒

public class UserMessage
{
            public string id { get; set; }
            public DateTime updated_time { get; set; }
            public string snippet { get; set; }
            public List<dat> participants { get; set; }
}
public class dat
{
            public string name { get; set; }
            public string email { get; set; }
            public string id { get; set; }
}

 List<UserMessage> lma1 = xd1.Descendants("data").Select(d => new UserMessage()
 {

                id = d.Element("id").Value,
                updated_time = Convert.ToDateTime(d.Element("updated_time").Value),
                snippet = d.Element("snippet").Value,
                participants = d.Descendants("dat").Select(f => new dat()
                {
                    email = f.Element("email").Value,
                    id = f.Element("id").Value,
                    name = f.Element("name").Value,
                }).ToList().Where(m => m.email != "facebook.com").Select(m => new dat()
                {
                    email = m.email,
                    id = m.id,
                    name = m.name,
                }).ToList(),

}).ToList();

2014年6月26日 星期四

JavaScript Hello World

前言就不需要了,想看歷史Google一下吧!

開發工具用Chrome就可以了,打開Chrome 按F12,上面的工具列選擇Console
輸入:
alert("Hello World")
或 console.log("Hello World")
或 document.write("Hello World")

講一下這三種使用上差別:
1: alert("Hello World") 這個很經常使用到,我們再開發的時候如果需要提示使用者,或是一些注意事項的時候可以使用這個方法。還有另一個常用的語法
if(confirm("我帥嗎?")) { alert("不帥!"); } else { alert("帥!"); }


2: console.log("Hello World")
這個方法也很常使用到,我們再寫flow的時候就可以透過這個方式Debug,將我們的處理過程顯示出來。

3: document.write("Hello World")
這個就很少再使用了,用這個語法會把所有的資料全部清掉,只秀出Hello World



2014年6月25日 星期三

JQuery CheckBox 全選

if($("#clickAll").prop("checked"))
{
   $("input[name='user_active_col[]']").each(function() {
         $(this).prop("checked", true);
   });
}
else
{
   $("input[name='user_active_col[]']").each(function() {
         $(this).prop("checked", false);
   });          
}

2014年6月23日 星期一