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();
        }
    }
}