Posts

Showing posts from 2011

C# - DateTime To String

String _display = DateTime.Now.ToString("dd/MM/yyyy"); _display result is: 27/12/2011 You can display by .NET Datetime format characters as the following: d - Numeric day of the month without a leading zero. dd - Numeric day of the month with a leading zero. ddd - Abbreviated name of the day of the week. dddd - Full name of the day of the week. f,ff,fff,ffff,fffff,ffffff - Fraction of a second. The more Fs the higher the precision. h - 12 Hour clock, no leading zero. hh - 12 Hour clock with leading zero. H - 24 Hour clock, no leading zero. HH - 24 Hour clock with leading zero. m - Minutes with no leading zero. mm - Minutes with leading zero. M - Numeric month with no leading zero ex.2. MM - Numeric month with a leading zero ex.02. MMM - Abbreviated name of month ex.Dec MMMM - Full month name ex.December. s - Seconds with no leading zero. ss - Seconds with leading zero. t - AM/PM but only the first letter. tt - AM/PM ( a.m. / p.m...

MSSQL - Set permission to update database MSSQL2008

In MSSQL2008 Server, when you change/edit table schema, sometimes you can not save change. The error you found is " Saving change is not permitted . The changes you have made require the following tables to be dropped and re-created....." You can fixed this problem with this solution. Solution: - In SQL Server Management Studio > Go to Tools Menu > Options > Designers - Find the option "Prevent saving changes that require a table re-creation" and unchecked it. Now, you can save changed your new schema.

C# - Convert Image URL To BASE64

public String ConvertImageURLToBase64(String url) {      StringBuilder _sb = new StringBuilder();      Byte[] _byte = this.GetImage(url);      _sb.Append(Convert.ToBase64String(_byte, 0, _byte.Length));      return _sb.ToString(); } private byte[] GetImage(string url) {      Stream stream = null;      byte[] buf;      try      {           WebProxy myProxy = new WebProxy();           HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);           HttpWebResponse response = (HttpWebResponse)req.GetResponse();           stream = response.GetResponseStream();  ...

MSSQL - how to change "SA" password

1. Open the "SQL Server Enterprise Manager". This is usually under "Start"-->"Programs"-->"Microsoft SQL Server". 2. Navigate to the "Logins" object under the "Security" folder on the SQL Server you wish to administer. Then, right click on the 'sa' account and select "Properties". 3. Now, enter a new password in the "Password" field under the "Authentication" options. website reference: http://www.eukhost.com/forums/f31/how-change-mssql-sa-password-511

MSSQL - select a random row

Microsoft SQL Server provide function "NEWID()" to random record from database. Let's see example. SELECT TOP 1 column FROM table ORDER BY NEWID() Example: SELECT TOP 1 code_item FROM game_item WHERE available=1 ORDER BY NEWID()

MSSQL - how to select all table name

In Oracle, you can use statement "select * from tab" for get all table name in your database. But for MSSQL you have to use "select * from sys.Tables" instead of "tab". Example. USE nothwinds SELECT * FROM SYS.TABLES

HTML - Image Lightbox

Image
If you need to show the full size overlay image on the current page. "Lokesh Dhakar"'s coding is a good example. His code is on javascript and stylesheet. Please visit Lokesh's site to download & see the example. URL Reference: http://www.huddletogether.com/projects/lightbox2/ Example: if you click on thumbnail image. You'll get the full image like this.

MSSQL - Reset Identity Column Value in SQL Server

Command to reset identity column: DBCC CHECKIDENT(table_name,RESEED,0) 1 is the next value of your identity column. Example. Column "id" in table "customer" is an identity column. DBCC CHECKIDENT("customer",RESEED,0) -> next id is 1. or DBCC CHECKIDENT("customer",RESEED,10) -> next id is 11. If you need to check next ID you can use DBCC CHECKIDENT("customer",NORESEED) -> you'll get these result. "Checking identity information: current identity value '0', current column value '0'. DBCC execution completed. If DBCC printed error messages, contact your system administrator." Or you can use this SQL statement to get the current identity: Example. SELECT IDENT_CURRENT ('customer') AS Current_Identity

javascript - show/hide div section

< script language = "javascript" > function toggle ( ) { var ele = document. getElementById ( "toggleText" ) ; var text = document. getElementById ( "displayText" ) ; if ( ele. style . display == "block" ) { ele. style . display = "none" ; text. innerHTML = "show" ; } else { ele. style . display = "block" ; text. innerHTML = "hide" ; } } </ script >   <a id="displayText" href="javascript:toggle();">show</a> <== click Here <div id="toggleText" style="display: none"><h1>peek-a-boo</h1></div> web reference: www.randomsnippets.com

MSSQL - show data per page

- This statement will return record 1 to 20. SELECT * FROM ( SELECT row_number() OVER (ORDER BY id) AS rownum, * FROM example_table_a ) AS A WHERE A.rownum BETWEEN (1) AND (20) - If you want the next 20, your where criteria should be: WHERE A.rownum BETWEEN (21) AND (40) - Finally, SQL statement in your C# code will be: public String CreateSQL(Int32 pageNo, Int32 pageSize) {      StringBuilder _sbSQL = new StringBuilder();      Int32 _offset = 1+ ((pageNo-1) * pageSize);      _sbSQL.Append(" SELECT * FROM ");      _sbSQL.Append(" ( ");      _sbSQL.Append(" SELECT row_number() OVER (ORDER BY id) AS rownum, * ");      _sbSQL.Append(" FROM example_table_a ");      _sbSQL.Append(" ) AS A ");      _sbSQL.Append(" WHERE A.rownum BETWEEN (" + _offset +") AND (" + (_offse...

MSSQL - Integer Type

- bigint Integer (whole number) data from -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807). Storage size is 8 bytes. - int Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647). Storage size is 4 bytes. The SQL-92 synonym for int is integer. - smallint Integer data from -2^15 (-32,768) through 2^15 - 1 (32,767). Storage size is 2 bytes. - tinyint Integer data from 0 through 255. Storage size is 1 byte. Remarks The bigint data type is supported where integer values are supported. However, bigint is intended for special cases where the integer values may exceed the range supported by the int data type. The int data type remains the primary integer data type in SQL Server. bigint fits between smallmoney and int in the data type precedence chart. Functions will return bigint only if the parameter expression is a bigint data type. SQL Server will not automatically promote other integer data types (tinyint, smal...

PHP - Fatal error: Call to undefined function curl_init()

Fatal error : Call to undefined function curl_init() Solution of this problem is we need open "curl function" first. We can open this function in "php.ini" in "C:\windows\php.ini" step 1: open "c:\windows\php.ini" step 2: find ";extension=php_curl.dll" and change to "extension=php_curl.dll" step 3: go to "dir: \AppServ\php5" step 4: find     -libeay32.dll     -ssleay32.dll step 5: copy those files to "C:\windows\system32"; step 6 (last step): restart apache or restart appserv server. Website reference:  moshikub.com

PHP - MD5

string md5 ( string $str [, bool $raw_output = false ] ) output of md5 function will returns the hash as a 32-character hexadecimal number. But if you set $raw_output = true , output will return in binary format with a length of 16. Example #1: $str = 'tmvh:partner01:12345'; $output_md5 = md5($str); echo $output_md5; //output is "3d5e420dc4eaaef230409fad5662e825" Example #2 : $str = 'tmvh:partner01:12345'; $output_md5 = md5($str,true); //$output_md5 contain binary format //Convert $md5_output binary format to BASE64 $output_base64 = base64_encode($output_md5); echo $output_base64; //output is "PV5CDcTqrvIwQJ+tVmLoJQ=="

HTML - ToolTip

Step1. Add "htmltooltip.js" into <head> tag in your HTML See "htmltooltip.js" source code here Step2. Insert this stylesheet script in <head> tag in your HTML <style type="text/css">      div.htmltooltip {      position: absolute; /*leave this and next 3 values alone*/      z-index: 1000;      left: -1000px;      top: -1000px;      background: #EEEEEE;      border: 2px solid black;      color: black;      padding: 3px;      width: 250px; /*width of tooltip*/      } </style> In <body> tag. Add your tooltips text to <div class="htmltooltip"></div>. Example. <a href="#" rel="htmltooltip"> Displaying My tooltip text </a> <div class="htmlt...

PHP - cURL (post by XML)

<?php //this url return result in xml format $url = " http://www.exmple.com/fakeResponse.aspx "; $xml_request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>  <note>  <to>Tove</to>  <from>Jani</from>  <heading>Reminder</heading>  <body>Don't forget me this weekend!</body>  </note>"; $response = curl_xml($url, $xml_request); /* ------- Response  -------*/ //header for response result in xml format header('Content-type: text/xml'); echo $response; /* ------- CURL_XML -------*/ function curl_xml($_url, $_xmlRequest) {     $header = array("content-type: text/xml");     // initialize curl handle     $c = curl_init();     // set url to post to     curl_setopt($c, CURLOPT_URL, $_url);     //include header as needed     curl_setopt($c, CURLOPT_HTTPHEADER, $header)...

PHP - cURL (post by parameter)

<?php //this url return result in xml format $url = http://www.example.com/receiveRequest.php ; $parameter = "name=henry"; $response = curl_xml($url, $parameter); /* ------- Response -------*/ //header for response result in xml format header('Content-type: text/xml'); echo $response; /* ------- CURL_XML -------*/ function curl_xml($_url, $_xmlRequest) {     $header = array("content-type: application/x-www-form-urlencoded");     // initialize curl handle     $c = curl_init();     // set url to post to     curl_setopt($c, CURLOPT_URL, $_url);     //include header as needed     curl_setopt($c, CURLOPT_HTTPHEADER, $header);     //not include the header in th output     curl_setopt($c, CURLOPT_HEADER, 0);     // set post method in request otherwise curl will do a GET request     curl_setopt($c, CURLO...

PHP - HTTP Post

function curl_xml($_url, $_xmlRequest) {     $header = array("content-type: text/xml");     $c = curl_init();// initialize curl handle     curl_setopt($c, CURLOPT_URL, $_url);// set url to post to     curl_setopt($c, CURLOPT_HTTPHEADER, $header);//include header as needed     curl_setopt($c, CURLOPT_HEADER, 0);//not include the header in th output     curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); //set follow redirect send by the server     curl_setopt($c, CURLOPT_MAXREDIR, 5); // Limit redirections to four     curl_setopt($c, CURLOPT_POST, 1);// set post method in request otherwise curl will do a GET request     curl_setopt($c, CURLOPT_POSTFIELDS, $_xmlRequest);// add POST fields     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);// return into a variable     curl_setopt($c, CURLOPT_TIMEOUT, 8); // times out after 8s ...

C# - Encoding & Decoding Text

Example for Convert Encoding from "ISO-8859-11" to UTF8 String _outputString = this.ConvertToUTF8("test 123", "ISO-8859-11"); or convert from "ISO-8859-11" to "windows-874" or another encoding String _outputString = this.ConvertString("test 123", "ISO-8859-11", "windows-874"); public String ConvertToUTF8(String inputString, String inputEncoding) {      String _output = null;      Byte[] _byteArray = System.Text.Encoding.GetEncoding(inputEncoding).GetBytes(inputString);      _output = System.Text.Encoding.UTF8.GetString(_byteArray);      return _output; } public String ConvertToString(String inputString, String inputEncoding, String outputEncoding) {      String _output = null;      Byte[] _byteArray = System.Text.Encoding.GetEncoding(inputEncoding).GetBytes(inputString);      _output = System.Te...

C# - Random Example

If you need to random number between 1 to 10. You can use "Random.Next(minimumValue,maximumValue)". Remark that if maximum equal 10, the random number can be 0-9. If you want 10 in your random list, you have to set the maximum number is 11. Random _random = new Random(); Int32 _iRandom = _random.Next(1, _maximumValue + 1); //_iRandom can be 1-10. Example. Unique random number private String RandomList(Int32 totalLuckyWinner, Int32 totalRecord) {      #region [ variable ]      String _strRandom = null;      #endregion      #region [ random number ]      if (totalRecord > 0)      {           Random _random = new Random();           Int32 _iRandom = 0;           Int32...

C# - String to BASE64 and vise versa

public String StringToBASE64(String message) {      String _strBASE64 = null;      Byte[] _byteArray = System.Text.Encoding.UTF8.GetBytes(message);      _strBASE64 = Convert.ToBase64String(_byteArray);      return _strBASE64; } public String BASE64ToString(String message) {      String _strResult = null;      Byte[] _byteArray = Convert.FromBase64String(message); javascript:void(0)      _strResult = System.Text.Encoding.UTF8.GetString(_byteArray);      return _strResult; }

javascript - htmltooltip.js

//Inline HTML Tooltip script: By JavaScript Kit: http://www.javascriptkit.com //Created: July 10th, 08' var htmltooltip={      tipclass: 'htmltooltip',      fadeeffect: [true, 500],      anchors: [],      tooltips: [], //array to contain references to all tooltip DIVs on the page      positiontip:function($, tipindex, e){           var anchor=this.anchors[tipindex]           var tooltip=this.tooltips[tipindex]           var scrollLeft=window.pageXOffset? window.pageXOffset : this.iebody.scrollLeft           var scrollTop=window.pageYOffset? window.pageYOffset : this.iebody.scrollTop           var docwidth...

javascript - AJAX

function loadAJAX(url) {      try {         var xmlhttp;         if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari            xmlhttp = new XMLHttpRequest();         }         else {// code for IE6, IE5            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");         }         xmlhttp.onreadystatechange = function() {            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {               alert(xmlhttp.responseText);    ...

javascript - isNumeric

function isNumeric(n) {      var _isNumeric = !isNaN(parseFloat(n)) && isFinite(n);      if (_isNumeric)         return true;      else         return false; }

C# - Hex To String

public String HexToString(String hexString) {      int value;      string stringValue = null;      char charValue;      string _strOutput = null;      string hexValues = hexString;      string _leftPosition = null;      Int32 _subLength = 0;      string _tmp = null;      string[] hexValuesSplit;      for (Int32 _i = 0; _i < hexValues.Length; _i=(_i+_subLength))      {         _leftPosition = hexValues.Substring(_i, 1);         switch (_leftPosition.ToUpper())         {            case "E":      ...

C# - String To Hex

public String StringToHex(String input) {      string hexOutput = null;      int value;      char[] values = input.ToCharArray();      foreach (char letter in values)      {         // Get the integral value of the character.         value = Convert.ToInt32(letter);         // Convert the decimal value to a hexadecimal value in string form.         hexOutput += String.Format("{0:X}", value);         //Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);      }      return hexOutput; }

C# - MD5 in BASE64

public String encriptStringByMD5(String input) {      String output;      //The encoder class used to convert strPlainText to an array of bytes      Encoding enc = Encoding.GetEncoding("windows-874");      //Create an instance of the MD5CryptoServiceProvider class      MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();      //Call ComputeHash, passing in the plain-text string as an array of bytes      //The return value is the encrypted value, as an array of bytes      Byte[] hashedDataBytes = md5Hasher.ComputeHash(enc.GetBytes(input));      output = Convert.ToBase64String(hashedDataBytes,0,hashedDataBytes.Length );      //output = BitConverter.ToString(hashedDataBytes);      return o...

C# - BASE64 to Image

public void ConvertBase64ToImage(String strBASE64) {      String _outputFile = @"c:\temp\test.jpg";      try      {         Byte[] _imageBytes = Convert.FromBase64String(strBASE64);         MemoryStream ms = new MemoryStream(_imageBytes, 0, _imageBytes.Length);         // Convert byte[] to Image         ms.Write(_imageBytes, 0, _imageBytes.Length);         Image _image = Image.FromStream(ms, true);         _image.Save(_outputFile);         MessageBox.Show("save image success");      }      catch (Exception ex)      {      ...