Tuesday, 12 October 2010

How to Print Number in Currency Format using C#

Steps for this Program:

1. Use {0:C} to print double number in currency format.

2. This is applicable for all C# projects , either web or windows.

Program to Print values in Currency format:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestConsole
{
    class CurrencyFormat
    {
        static void Main(string[] args)
        {
            double myCurrency = 123456.9854;

            Console.WriteLine("The given number is: {0}", myCurrency);
            Console.WriteLine("The given number in currency format is: {0:C}", myCurrency);
            Console.ReadLine();
        }
    }
}

Output of Currency Format is:

How Print Number in Currency Format using C#

How to use JavaScript onerror Event

Steps to write JavaScript Program:

1. Write a JavaScript function  (Errorhandling) to handle the error.

2. Write onerror=Errorhandling; to pass the control whenever there is error.

Code to use onerror event:

<html>
<head>
<script type="text/javascript">
onerror=Errorhandling;
var My_Error="";

function Errorhandling(msg,url,l)
{
My_Error="Error Details:\n\n";
My_Error+="Error: " + msg + "\n";
My_Error+="URL: " + url + "\n";
My_Error+="Line: " + l + "\n\n";

document.write(My_Error);
}

function Click()
{
adddlert("Error Handling!");
}
</script>
</head>

<body>
<input type="button" value="Click" onclick="Click()" />
</body>

</html>

Output:

How to use JavaScript onerror Event

How to Run this program:

1. Copy the code above and paste to notepad file

2. Save file as SomeFileName.html (Please note that give file extension to .html or .htm)

3. Open this file in Internet explorer.

How to Handle Error in JavaScript | Try Catch Block

JavaScript Coding Steps for Error Handling:

1. Use try block outside the code and handle errors in catch block as given below.

2. You can log the error for debugging purpose.

Try Catch Blocks in JavaScript:

<html>
<head>
<script type="text/javascript">
var My_Error="";
try
  {
  adddlert("This is JavaScript Try Catch Demo!");
  }
catch(ex)
  {
  My_Error="You have reached to catch block.\n\n";
  My_Error+="Error description: " + ex.description + "\n\n";
  alert(My_Error);
  }
</script>
</head>
</html>

Try Catch block Output:

How to Handle Error in JavaScript | Try Catch Block

How to Run this program:

1. Copy the code above and paste to notepad file

2. Save file as SomeFileName.html (Please note that give file extension to .html or .htm)

3. Open this file in Internet explorer.

How to Write First JavaScript Program

Coding Steps:

1.  you need to write your JavaScript code inside the script blocks

<script type="text/javascript"> and </script>

2. use JavaScript function document.write() to print any value on screen. pass value inside the function.

First JavaScript program Code:

<html>
<body>
<script type="text/javascript">
document.write("My First JavaScript Program.!");

</script>
</body>
</html>

Output of JavaScript program:

How to Write First JavaScript Program

How to Run this program:

1. Copy the code above and paste to notepad file

2. Save file as SomeFileName.html (Please note that give file extension to .html or .htm)

3. Open this file in Internet explorer.

Thursday, 30 September 2010

Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'?

Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'?

While implementing the email functionality from my website, suddenly i recieve this error.

MailMessage m = new MailMessage();
m.From = "abc@indihub.com";

in second line i received this error. this is because you can not directly convert the string type to mail data type. To fix this error you need to convert you string email address to mail data type.

Same you can achieve by

MailMessage m = new MailMessage();
m.From = new System.Net.Mail.MailAddress("abc@indihub.com");

This line will create a object of mail. Where MailAddress() is a class constructor, takes an email address as an input parameter.

Friday, 24 September 2010

How to Program foreach Loop in C#

This program demonstrate the use of foreach loop. you can use this loop with any collection and you can not modify values while looping.

image

Source Code of ForEach loop:

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsForEachLoop
    {
        public static void Main()
        {
            try
            {
                string[] Sites = { "Csharptalk.com", "IndiHub.com", "BharatClick.com", "Csharphub.com", "Sharepointbank.com" };

                foreach (string EachSite in Sites)
                {
                    Console.WriteLine("{0}", EachSite);
                }
            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}

C# program for Switch Statement | Switch � Case Example

This is a switch program to demonstrate the use if switch statement in C#. We have used two cases together to show multiple cases for one options. in place of integer options you are allowed to use other data types.

C# program for Switch Statement | Switch – Case Example

C# program for Switch Statement | Switch – Case Example

Source Code of Switch Statement:

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsIfElase
    {
        public static void Main()
        {
            try
            {               
                int InputNumber;
                Console.Write("Please enter a number: ");
                InputNumber = Int32.Parse(Console.ReadLine());              

                switch (InputNumber)
                {
                    case 1:
                    case 4:
                        Console.WriteLine("Number is {0}.", InputNumber);
                        break;
                    case 2:
                        Console.WriteLine("Number is {0}.", InputNumber);
                        break;
                    case 3:
                        Console.WriteLine("Number is {0}.", InputNumber);
                        break;
                    default:
                        Console.WriteLine("Number {0} is not between 1 and 4.", InputNumber);
                        break;
                }

            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}