Wednesday, September 28, 2011

Simple Extension Methods: C# Programming

Simple Extension Methods: C# Programming

1. Create a namespace for the extension methods. (i.e. ExtensionMethods)
2. Cteate a class. (i.e. MyExtensions)
3. Now create the extension methods in the class.

Here, we are extending string type to have a new extension method 'WordCount':

using System;
using System.Text;

namespace ExtensionMethods
{
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}


4. Now, lets use this extension method:

using System;
using ExtensionMethods;

namespace ThreadTest
{
class Program
{

static void Main(string[] args)
{
string str = "You are rocking";
Console.WriteLine(str.WordCount());
}
}
}

5. That's it. Happy coding.

Wednesday, August 10, 2011

Sql Server - Add linked server via script.


exec sp_addlinkedserver 'ServerName\DATABASE,3262'

Tuesday, June 21, 2011

Findout the recently modified objects in sql database

SELECT name, create_date, modify_date

FROM sys.objects

WHERE type = 'P'

order by modify_date desc

Monday, June 20, 2011

Find a string in all Stored Procedures

Find a string in all Stored Procedures:

SELECT OBJECT_NAME(id)
FROM syscomments
WHERE [text] LIKE '%foobar%'
AND OBJECTPROPERTY(id, 'IsProcedure') = 1
GROUP BY OBJECT_NAME(id)

Wednesday, March 23, 2011

Send mail from asp.net

1. Create a MailHelper class.

public class MailHelper
    {
        public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
        {
            MailMessage mMailMessage = new MailMessage();
            mMailMessage.From = new MailAddress(from);
            mMailMessage.To.Add(new MailAddress(to));

            if (!(string.IsNullOrEmpty(bcc)))
            {
                mMailMessage.Bcc.Add(new MailAddress(bcc));
            }

            if (string.IsNullOrEmpty(cc))
            {
                mMailMessage.CC.Add(new MailAddress(cc));
            }

            mMailMessage.Subject = subject;
            mMailMessage.Body = body;
            mMailMessage.IsBodyHtml = true;
            mMailMessage.Priority = MailPriority.Normal;

            // Instantiate a new instance of SmtpClient
            SmtpClient ss = new SmtpClient();
            //ss.EnableSsl = true;
            ss.Timeout = 10000;
            ss.DeliveryMethod = SmtpDeliveryMethod.Network;

            mMailMessage.BodyEncoding = UTF8Encoding.UTF8;
            mMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            ss.Send(mMailMessage);
        }

2. Add the following smtp configuration in Web.config file.


 
 

Wednesday, March 16, 2011

My system is damn slow, what to do?

This post is an initiative for tips to improve the speed of system running Windows 7, so that we dont waste the system resources:

1. RacTask : Microsoft Reliability Analysis task to process system reliability data.
If you want to speed up your task rather than this analysis, diable this task in TaskScheduler.
Task Scheduler > Task Scheduler Library > Microsoft > Windows > RAC > RacTask > right click > disable

2.

Tuesday, February 1, 2011

Session state can only be used when enableSessionState is set to true

asp.net issue:
Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that system.Web.SessionStateModule or a custom session statemodule is included in the \\ section in the application configuration.

One possible solution - Set the startup page in your website.