This blog has been moved!

This blog has been discontinued. Please visit this site for more awesome stuff
-> http://aivan.io

Add www to your site automatically

We will be using .htaccess which should be located on the root of your website

RewriteEngine on
Options FollowSymlinks
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

How to display twitter content on your page

We will be using twitterjs for this demo

	<script type='text/javascript' src='js/twitter-1.13.min.js'></script>
	<script type="text/javascript" charset="utf-8">
	getTwitters('tweet', { 
	  id: 'enter_your_id_here', 
	  count: 1, 
	  enableLinks: true, 
	  clearContents: true,
	  template: '%text% <div id="meta">posted %time% | <a href="http://twitter.com/%user_screen_name%/statuses/%id%/">Follow me</a> on twitter!</div>'
	});
	</script>

The count is set to 1 if you only like the latest post to be shown.
This is a div for my twitter code to access.

			<div id="twitter">
				<div id="tweet">
					<img src="images/loader.gif" alt="Loading..." height="16" width="16"/>
				</div><!-- end tweet -->
			</div>

Here is the css for the twitter styles:

/* Twitter styles */

#twitter {
	background:transparent url(images/twitter-trans.png) no-repeat;
	height:145px;
	padding-left:155px;
	padding-right:18px;
	padding-top:30px;
	width:697px;
	position:absolute;
	top:519px;
}

#tweet {
	font-size:19px;
}

#tweet ul {
	list-style:none;
	margin:0px;
	padding:0px;
}

#meta {
	font-family:georgia, helvetica, verdana, sans-serif;
	font-size:15px;
	font-style:italic;
	margin-right:15px;
	position:absolute;
	right:0;
	top:96px;
	color:#888;
}

Transmit files between two WebAPP

Server Application
First you have to build the Web Service, add this to the application where you want the file to be saved.

[WebMethod]
public bool SaveDocument( Byte[] docbinaryarray, string docname)
{
	string strdocPath;
	strdocPath = "C:\\DocumentDirectory\\" + docname;
	FileStream objfilestream =new FileStream(strdocPath,FileMode.Create,FileAccess.ReadWrite);
	objfilestream.Write(docbinaryarray,0,docbinaryarray.Length);
	objfilestream.Close();

	return true;
}

NOTE: The file will be saved to C:\\DocumentDirectory\\ just change the location. I suggest creating a key on your web.config to dynamically set this directory.

Client Application
Next, add this to the send button. Don’t forget to add “System.IO” to your references.

string sFile = "<file path>";

private void button1_Click(object sender, System.EventArgs e)
{
	FileStream objfilestream = new FileStream(sFile,FileMode.Open,FileAccess.Read);
	int len = (int)objfilestream.Length;
	Byte[] mybytearray = new Byte[len];
	objfilestream.Read(mybytearray,0,len);
	localhost.Service1 myservice = new localhost.Service1();
	myservice.SaveDocument(mybytearray,sFile.Remove(0,sFile.LastIndexOf("\\")+1));
	objfilestream.Close();
}

NOTE: sFile is the file path of the document you want to send to the server.

Share files without having a workgroup

Logon failure: the user has not been granted the requested logon type at this computer
If you see this message the following should be done on the computer containing the shared files:
Download the following and install it

Windows Server 2003 Resource Kit Tools

Then enter the following commands. (Attention: they are case sensitive.)

net user guest /active:yes
ntrights +r SeNetworkLogonRight -u Guest
ntrights -r SeDenyNetworkLogonRight -u Guest

The first command enables network access for Guest, the two subsequent ones change two different policies to allow network access for Guest.

Get Parameter Value from QueryString

This is a javascript snippet to accomplish such

        function getParameterByName(name)
        {
          name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
          var regexS = "[\\?&]"+name+"=([^&#]*)";
          var regex = new RegExp( regexS );
          var results = regex.exec( window.location.href );
          if( results == null )
            return "";
          else
            return results[1];
        }

Enable C# 3.0 on VS2005 ASP.NET

Add this to you web.config after </system.web>

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
    </compilers>
  </system.codedom>

Check All checkbox field for ASP.NET GridView

This is a code snippet is for the gridview rows:

        $("[id$='chkRow']").click(
        function(){
	      var prev;
	      var status = $(this).attr('checked');
                prev = $(this).parent().next().children().attr('checked',status)
                prev = $(prev).parent().next().children().attr('checked',status)
	            prev = $(prev).parent().next().children().attr('checked',status)
	            prev = $(prev).parent().next().children().attr('checked',status)
                prev = $(prev).parent().next().children().attr('checked',status)
        });

This is code is for the gridview columns

        $("[id$='chkView']").click(
        function(){
                var status = $(this).attr('checked')
	        $("[id$='ViewCheck']").attr('checked',status)
        });

MySQL date format in C#

This is a method to convert any DateTime object to MySQL DateTime format:

        public static string ConvertToMySQLDate(DateTime date)
        {
            return (date.ToString("yyyy-MM-dd HH:mm:ss"));

        }

How to access values of HTML controls with runat server property

You could access the values of this controls using

Request.Form[Control.UniqueID];

The Form collection retrieves the values of form elements posted to the HTTP request body, with a form using the POST method.