Sep 27, 2007

Application Instance is already running in C#

How to make sure that only one instance of application is running in C#

 

 

public static void Main(string[] args)

        {

// ----------------------------------------------------

            bool running;

            Mutex mutex = new Mutex(true, “Your application name”, out running);

 

            if (!running)

            {

                Console.WriteLine("Another instance is already running.");

                Console.WriteLine (Messages.ExitMessage);

                Console.ReadKey(false);

                return;

            }

// -----------------------------------------------------

 

 

Your all working code will go here

 

 

// -------- Don’t forget to include this

 

            GC.KeepAlive(mutex);  //1

 

            mutex.ReleaseMutex(); //2

  }

 

Note: Use only one either 1 or 2 both will works, but make sure one is present.

 

Now only one application will run at a time.

May 2, 2007

How to get the first not null column from a table?

Hey , really a tricky question , select all the values into a single column from a table which is first not null in that table.
For Exapmle
There is table test





idcol1col2col3col4
1NULLNULLWM
2QNULL RNULL
3 NULLWPT
4NULLNULL NULLS


Now I want to get the result like that in a single column
First Not NULL Values
W
Q
W
S

So what you think how can we get that,
First thing is that you can use cursor and loop each row and get them.
The second one is the use of COALESCE to get that

this query will get the result

SELECT COALESCE(col1,col2,colo3,col4) FROM TEST

If you have any query you can ask me at matespoint@gmail.com

Mar 29, 2007

Sending Email thru C#

Sending email thru C#, .NET 2.0 , with attachment,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;

namespace MailConnect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Sendbutton_Click(object sender, EventArgs e)
{
MailMessage aMessage = new MailMessage("matespoint@gmail.com", "xyz@yahoo.com");
aMessage.Subject = "This is test message";
aMessage.Body = "Body text message";

Attachment aT = new Attachment("C:\files.doc");
aMessage.Attachments.Add(aT);
aT = new Attachment("C:\files.pdf");
aMessage.Attachments.Add(aT);

SmtpClient aClient = new SmtpClient(servername, 25);
aClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
aClient.Send(aMessage);
MessageLabel.Text = "Mail has been sent successfully";
}
}
}

Mar 22, 2007

Concatenate strings from a column into a single row?

CREATE DATABASE Test1
Go
Use Test1
set nocount on
CREATE TABLE dbo.Test2
(
Col1 int identity,
Col2 varchar (5) default('aaaaa')
)
declare @loop
int set @loop = 1
while @loop <= 100
begin
insert into Test2 default values
set @loop = @loop + 1
end


Now you had create a Database Test1 and a Table Test2 with 100 records

Now concatenate this to a single string

DECLARE @ConcatString varchar(8000)
SELECT @ConcatString = COALESCE(@ConcatString + ',','' ) + CAST(Col1 as varchar(12))
FROM Test2
SELECT @ConcatString

You can find the same result by using ISNULL

DECLARE @ConcatString varchar(8000)
SELECT @ConcatString = ISNULL(@ConcatString + ',','' ) + CAST(Col1 as varchar(12))FROM Test2
SELECT @ConcatString


Enjoy .. .. better performance using this instead of using CURSOR.

Mar 16, 2007

Few Definitions

  1. School: A place where Papa pays and Son plays.
  2. Life Insurance: A contract that keeps you poor all your life so that you can die Rich.
  3. Nurse: A person who wakes u up to give you sleeping pills.
  4. Marriage: It's an agreement in which a man loses his bachelor degree and a woman gains her masters.
  5. Divorce: Future tense of Marriage.
  6. Tears : The hydraulic force by which masculine willpower is defeated byfeminine waterpower.
  7. Lecture: An art of transferring information from the notes of the Lecturer to the notes of the students without passing through "the minds of either"
  8. Conference: The confusion of one man multiplied by the number present.
  9. Compromise : The art of dividing a cake in such a way that everybody believes he got the biggest piece.
  10. Dictionary : A place where success comes before work.
  11. Conference Room : A place where everybody talks, nobody listens and everybody disagrees later on.
  12. Father : A banker provided by nature.
  13. Criminal: A guy no different from the rest....except that he got caught.
  14. Boss : Someone who is early when you are late and late when you are early.
  15. Politician : One who shakes your hand before elections and your Confidence after.
  16. Doctor : A person who kills your ills by pills, and kills you by bills.
  17. Classic : Books, which people praise, but do not read.
  18. Smile : A curve that can set a lot of things straight.
  19. Office : A place where you can relax after your strenuous home life.
  20. Yawn: The only time some married men ever get to open their mouth.
  21. Etc .: A sign to make others believe that you know more than you actually do.
  22. Committee : Individuals who can do nothing individually and sit to decide that nothing can be done together.
  23. Experience: The name men give to their mistakes.
  24. Atom Bomb : An invention to end all inventions.
  25. Philosopher : A fool who torments himself during life, to be spoken of whendead

Hope you enjoy ... Have fun ...

Write your definitions if you want to share one

Application Domain

An Application Domain is a light weight process and provides a logical and physical isolation from another .NET application.

  • This ensures that the Applications can work independent and isolated of each other. An Application Domain is created by the Common Language Runtime (CLR) and it ensures that if one Application Domain crashes or goes down, it does not in any way effect the functioning of another Application Domain.
  • Multiple .NET applications can be executed in one single process by loading these applications in separate Application Domains.
  • Several threads can be executing in a single application domain at any given time and a particular thread is not confined to a single application domain. In other words, threads are free to cross application domain boundaries and a new thread is not created for each application domain.
  • The following are the benefits of Application Domains.
    Isolation of code, data and configuration information of one application from another
    A failure in one application will not affect the other

Abstract Class Vs Interface

Abstract Class Vs Interface

There are a lot of discussion on the internet about the Interface vs Abstract class. and also , as BASE class whether we should use interface, abstract class or normal class.
Abstract Class vs Interface

We can not make instance of Abstract Class as well as Interface.
Here are few differences in Abstract class and Interface as per the definition.
Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).
Interface can only contain abstract methods, properties but we don’t need to put abstract and public keyword. All the methods and properties defined in Interface are
by default public and abstract.

using System;
namespace ConsoleApplication2
{
/// /// Summary description for Class2. ///
public abstract class A
{
int someInt;
string someString;
public A()
{ // // TODO: Add constructor logic here // }
public abstract void F();
}

public class B : A
{
public override void F()
{ Console.WriteLine("F in B"); }
}

interface IA
{

//int someInt; give error we cant have fields in interface
//string someString;
string P{get;set;}
void F();
}
public class C : IA
{
public C() { }
public string P
{
get {return _p;}
set {_p = value;}
}
public void F()
{
Console.WriteLine("Abstract F()");
}
private string _p; }
}

we can make the call like this..
B b = new B();
b.F();
A a = b;
a.F();
//A a = new A(); will give error as we cant instantiate
C c = new C();
c.F();
c.P="Test String";
Console.WriteLine(c.P.ToString());
IA d = c;
d.F();
Console.WriteLine(c.P.ToString());
Console.Read();

Output :
F in B
F in B
Abstract F()
Test String
Abstract F()
Test String

We can see abstract class contains private members also we can put some methods with implementation also. But in case of interface only methods and properties
allowed.
We use abstract class and Interface for the base class in our application.Now most important question: What should use : Interface force everybody to implement all things in that. So use it when u wanna force all the methods, properties, etc to implement.Abstract Class - limited you to create an instance of this class, and if you derived from this class you have to implement all abstract members to create an instance of this derived class. Also Abstract class should used when you have some functions which you must override and some which you can leave in this scenerio use abstract classes,

SQL - Difference between CAST,CONVERT and PARSE

TODO---