October 2011 | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Avoid white background when iframe loads

Monday

Get Rid of  White Flash when iframe Loads:

                                                               To avoid white background while iframe loads, you can hides iframe until its fully loaded. You can use the following code snippets to hide iframe when its loading.
<iframe style="visibility:hidden;" onload="this.style.visibility = 'visible';" src="../iframe.html" > </iframe>

Get image size in Kb using c#


Get image size in Kb in asp.net using c#:
                                                            Use FileInfo and the Length property to measure file size.  Here is the code for  get the file size in your C# program easily. The blow sample contain code for get image size in KB and MB.

  FileInfo fileInfo = new FileInfo(Server.MapPath("~/Images/Header.jpg"));
double 
fileSizeKB fileInfo.Length / 1024;
double 
fileSizeMB fileInfo.Length / (1024 1024);

Encryption/Decryption Using C#

Thursday

Encryption/Decryption of a file/string using C#:
                                                   You can use the following code for Encryption/Decryption of a file or  a string. Here you can Encrypt /Decrypt a file  or a string by using the following methods. You just pass parameters as input in to that methods and you got suitable output.

Decrypt File-to-File
Encrypt File-to-File
Encrypt String-to-File
Decrypt File-to-String


Code:
using System;
using 
System.Collections.Generic;
using 
System.Text;
using 
System.Security.Cryptography;
using 
System.IO;

namespace 
BHI.Rats
{

    
/// <summary>
    /// RatsEncryptionManager is responsible for encrypting and decrypting the files.
    /// </summary>
    
public static class  RatsEncryptionManager
    {
        
private const string passKey "c0ntr0l1";
        
/// <summary>
        /// Decrypts the input file (strInputFileName) and creates a new decrypted file (strOutputFileName)
        /// </summary>
        /// <param name="strInputFileName">input file name</param>
        /// <param name="strOutputFileName">output file name</param>
        
public static void DecryptFiletoFile(string strInputFileName, string strOutputFileName)
        {
            
string strFileData "";
            using 
(FileStream inputStream = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read))
            {
                DESCryptoServiceProvider cryptic 
= new DESCryptoServiceProvider();
                
cryptic.Key ASCIIEncoding.ASCII.GetBytes(passKey);
                
cryptic.IV ASCIIEncoding.ASCII.GetBytes(passKey);
                
CryptoStream crStream = new CryptoStream(inputStream, cryptic.CreateDecryptor(), CryptoStreamMode.Read);
                
StreamReader reader = new StreamReader(crStream);
                
strFileData reader.ReadToEnd();
                
reader.Close();
                
inputStream.Close();
            
}
            
if (File.Exists(strOutputFileName))
            {
                File.Delete(strOutputFileName)
;
            
}
             
using (StreamWriter outputStream = new StreamWriter(strOutputFileName))
            {
                 outputStream.Write(strFileData, 
0, strFileData.Length);
                
outputStream.Close();
            
}
         }

        
/// <summary>
        /// Encrypts the input file(strInputFileName) and creates a new encrypted file(strOutputFileName)
        /// </summary>
        /// <param name="strInputFileName">input file name</param>
        /// <param name="strOutputFileName">output file name</param>

        
public static void EncryptFiletoFile(string strInputFileName, string strOutputFileName)
        {
            
byte&#91;] fileBuffer;
            using 
(FileStream inputStream = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read))
            {
                fileBuffer 
= new byte&#91;inputStream.Length];
                
inputStream.Read(fileBuffer, 0, fileBuffer.GetLength(0));
                
inputStream.Close();
            
}
            
if (File.Exists(strOutputFileName))
            {
                 File.Delete(strOutputFileName)
;
            
}
             
using (FileStream outputStream = new FileStream(strOutputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                 DESCryptoServiceProvider cryptic 
= new DESCryptoServiceProvider();
                
cryptic.Key ASCIIEncoding.ASCII.GetBytes(passKey);
                
cryptic.IV ASCIIEncoding.ASCII.GetBytes(passKey);
                
CryptoStream crStream = new CryptoStream(outputStream, cryptic.CreateEncryptor(), CryptoStreamMode.Write);
                
crStream.Write(fileBuffer, 0, fileBuffer.Length);
                
crStream.Close();
            
}
        }

        
/// <summary>
        /// Encrypts the input string and creates a new encrypted file(strOutputFileName)
        /// </summary>
        /// <param name="strInputString">input string name</param>
        /// <param name="strOutputFileName">output file name</param>
        
public static void EncryptStringtoFile(string strInputString, string strOutputFileName)
        {
            
if (File.Exists(strOutputFileName))
            {
                 File.Delete(strOutputFileName)
;
            
}
            
using (FileStream outputStream = new FileStream(strOutputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                 DESCryptoServiceProvider cryptic 
= new DESCryptoServiceProvider();
                
cryptic.Key ASCIIEncoding.ASCII.GetBytes(passKey);
                
cryptic.IV ASCIIEncoding.ASCII.GetBytes(passKey);
                
CryptoStream crStream = new CryptoStream(outputStream, cryptic.CreateEncryptor(), CryptoStreamMode.Write);
                byte[
] buffer ASCIIEncoding.ASCII.GetBytes(strInputString);
                
crStream.Write(buffer, 0, buffer.Length);
                
crStream.Close();
            
}
         }

        
/// <summary>
         /// Decrypts the input file (strInputFileName) and creates a new decrypted file (strOutputFileName)
        /// </summary>
        /// <param name="strInputFileName">input file name</param>
        
public static string DecryptFiletoString(string strInputFileName)
        {
            
string strFileData "";
            using 
(FileStream inputStream = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read))
            {
                 DESCryptoServiceProvider cryptic 
= new DESCryptoServiceProvider();
                
cryptic.Key ASCIIEncoding.ASCII.GetBytes(passKey);
                
cryptic.IV ASCIIEncoding.ASCII.GetBytes(passKey);
                
CryptoStream crStream = new CryptoStream(inputStream, cryptic.CreateDecryptor(), CryptoStreamMode.Read);
                
StreamReader reader = new StreamReader(crStream);
                
strFileData reader.ReadToEnd();
                
reader.Close();
                
inputStream.Close();
            
}
            
return strFileData;
        
}
    }
}

CSS Only Buttons

Tuesday

Create buttons with pure css:
The following buttons created by using only css. But it look like  an image button, here you can see a fading effect while mouse over the button. 




Anchor

You can get the code from  here

Check if file exists using C#

Friday


C# File.Exists Method:

Exists method of the System.IO.File class can be used to check if a file exists in a given location.


Code:
private static bool IsFileExists(stringsFileName){
 try
 {
   if (File.Exists(sFileName) == true)
   {
     return true;
   }
   else
   { 
     return false;
   }
 }
 catch (Exception ex)
 {
   Console.WriteLine(ex.Message);
   return false;
 }
}

Get stored procedure count in sql server

Thursday


Some Important  SQL Queries:

                                     The following SQL queries  are very much used to my one of  a stored procedure based .net project. It may useful you too.


Get stored procedure count from a sql table:

SELECT count(*) SPCOUNT FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE='PROCEDURE'

Get number of function from a sql table:
SELECT count(*) FUNCTIONCOUNT FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE='FUNCTION'

Get all stored procedure name from a sql table:
SELECT name AS spname
FROM sysobjects
WHERE (xtype 'p'AND (name NOT LIKE dt%')
ORDER BY name 
Get all table name in a sql database:
Select table_name from information_schema.tables
where table_type='Base table' order by table_name 
Get stored procedure(s) for a sql table:
SELECT DISTINCT so.name
FROM 
syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE 
sc.TEXT LIKE '%table name%'

How to connect to MySQL database using PHP

Wednesday

Create a Connection to a MySQL Database:

                                                    Before you can get content out of your MySQL database, you must know how to establish a connection to MySQL from inside a PHP script. The following code explained the following concepts,


1. Connection to the database
2. Select a database
3. Execute the query
4. Fetch tha data
5. Close the connection


Sample Code:
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";

//connection to the database

 $dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with

$selected = mysql_select_db("examples",$dbhandle)
  or die("Could not select examples");

//execute the SQL query and return records

$result = mysql_query("SELECT id, model,year FROM cars");

//fetch tha data from the database

   while ($row = mysql_fetch_array($result)) {
   echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ".
   $row{'year'}."<br>";
}
//close the connection
mysql_close($dbhandle);
?>

Sending Email with Attachment in PHP

Sending Email with Attachment:
                                      To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email. Moreover, we want to use multipart/alternative MIME type to send both plain-text and HTML version of the email. Have a look at the example:

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash

$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks

$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>






As you can see, sending an email with attachment is easy to accomplish. In the preceding example we have multipart/mixed MIME type, and inside it we have multipart/alternative MIME type that specifies two versions of the email. To include an attachment to our message, we read the data from the specified file into a string, encode it with base64,  split it in smaller chunks to make sure that it matches the MIME specifications and then include it as an attachment.

Add Existing Frameworks in XCode 4.3

Tuesday


The blow steps used to add frameworks in XCode for your Iphone Apps development.

  1. In the project navigator, select your project
  2. Select your target
  3. Select the 'Build Phases' tab
  4. Open 'Link Binaries With Libraries' expander
  5. Click the '+' button
  6. Select your framework
  7. (optional) Drag and drop the added framework to the 'Frameworks' group




 
 
 

RECENT POSTS

Boost

 
Blogger Widgets