Sunday, August 30, 2009

How to Backup SQL Server Database Programatically in C#

SqlConnection SC = new SqlConnection("Place your connection string here");
string sqltxt ="";
string ipath ="C:\Backup.Bak";
sqltxt = @"BACKUP DATABASE SampleDB TO DISK = ' " + ipath + "' WITH INIT ";
 
 
SqlCommand sqlCm = new SqlCommand(sqltxt, SC);
SC.Open();
sqlCm.ExecuteNonQuery();
SC.Close();

Saturday, August 29, 2009

How to use list.Sum<> in System.Collections.Generic

List.Sum<>
This will return the summation of named attribute.
List<Item> listItem = new List<Item>();
 
Item T1 = new Item() { Name = "A", Amount = 100 };
Item T2 = new Item() { Name = "B", Amount = 600 };
Item T3 = new Item() { Name = "C", Amount = 100 };
Item T4 = new Item() { Name = "D", Amount = 150 };
Item T5 = new Item() { Name = "E", Amount = 200 };
 
listItem.Add(T1);
listItem.Add(T2);
listItem.Add(T3);
listItem.Add(T4);
listItem.Add(T5);
 
Decimal D = listItem.Sum(P=>P.Amount);
// sum of Amount
Conditional Sum
Assume that you want to get the summation of Amount lager than 100
Decimal D = listItem.Where(P=>P.Amount>100).Sum(P=>P.Amount);
List.Sum()
This will return the summation of the list.
List<int> intList = new List<int>();
 
intList.Add(2);
intList.Add(3);
intList.Add(4);
 
int sum = intList.Sum();
// sum of Amount

How to use list.find<> and list.findall<> in System.Collections.Generic

List.find<>
This will return first matched item from the list.
List. FindAll<>
This will return all matching items from the list.

Item Class Code
class Item
{
public int Amount;
public string Name;
}

List<Item> listItem = new List<Item>();
 
Item T1 = new Item() { Name = "A", Amount = 100 };
Item T2 = new Item() { Name = "B", Amount = 600 };
Item T3 = new Item() { Name = "C", Amount = 100 };
Item T4 = new Item() { Name = "D", Amount = 150 };
Item T5 = new Item() { Name = "E", Amount = 200 };
 
listItem.Add(T1);
listItem.Add(T2);
listItem.Add(T3);
listItem.Add(T4);
listItem.Add(T5);
Method 1
Item Result= listItem.Find(P=>P.Name=="A");
// item having "A" as it's item name
 
Item Result= listItem.Find(P=>(P.Name=="A" & P.Amount==100));
// item having "A" as it's item name and Amount as 100
 
Item Result= listItem.FindAll(P=>P.Amount==100);
//items having Amount as 100
Method 2
You can write your own function for searching.
private bool findItem(Item I) 
{
if (I.Name == "A")
{ return true; }
else
{ returnfalse;}
}
 
Item Result= listItem.Find(findItem);

Thursday, August 20, 2009

Simple LINQ Insert, Update , Delete Tutorial in C#

  • Open a new Visual Studio windows application project (c#)

  • Right click the project and goto AddNew item. and it will popup the following dialog.
  • select the Linq to SQL Classes and give a neme add click add. then double click the added class in your solution explorer. Then it will load following type of screen.

  • In the Data connection (Assumme you already created a SQL Server Database and also a table nemed "Table1"), Drag and drop the table to the window. Then it will ask for connection string saving dialog. For the moment keep it NO.
  • Now we will see how to use LINQ to do Insert , Update , Delete.
  • Coding is describing below. (sample is the name given by me to the LinqtoSQL class).

private void cmdAdd_Click(object sender, EventArgs e)
{
    sampleDataContext Dt = new sampleDataContext();
    Table1 T = new Table1();
    T.Address = "Address 1";
    T.Name = "a";
 
    Dt.Table1s.InsertOnSubmit(T);
    Dt.SubmitChanges();
}
 
private void cmdUpdate_Click(object sender, EventArgs e)
{
    sampleDataContext Dt = new sampleDataContext();
 
    Table1 t = Dt.Table1s.Single(p => p.Name == "a");
    t.Address = "address2";
    Dt.SubmitChanges();
}
 
private void cmdDelete_Click(object sender, EventArgs e)
{
    sampleDataContext Dt = new sampleDataContext();
 
    Table1 t = Dt.Table1s.Single(p => p.Name == "a");
    Dt.Table1s.DeleteOnSubmit(t);
    Dt.SubmitChanges();
}

Open Form (Push Form) in side a timer task in Blackberry

I tried several times to figure out how to push a form to a sreen inside a timer task. but meantime it gave me errors. but ultimately i found a way to do it.

The solusion is invokelater(). To run a UI we have to run our code in event-dispatching thred. 

 class RemindTask1 extends TimerTask 
 {
      public void run() 
      {

Runnable NextScreen = new Runnable()
{
   public void run()
         {
          UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
UiApplication.getUiApplication().pushScreen(new frmComplite());
   }
   };     

       UiApplication.getUiApplication().invokeLater(NextScreen);
     }
}

Saturday, August 15, 2009

Convert FAT32 to NTFS with out formating your harddisk

this is a simple nice command available in DOS commands. I had use this method in several occations with out any worries.  You can convert your FAT32 hard disk to NTFS with out formating or with out damaging your data.

goto Run ---->type cmd (or any how get theh commant prompt)
type convert c: /FS:NTFS (this will convert your C: drive to NTFS)

Wednesday, August 12, 2009

Public Key Cryptography in Windows Mobile Using RSA

Here shows how you can do public key cryptography encryption and decryption in windows mobile 6.0. In here stored the encrypted message and decrypt it using  private key.

public partial class frmRSA : Form

{

 

        byte[] encMsg;

        RSAParameters publickey;

        RSAParameters privatekey;

 

   private void menuKeyGen_Click(object sender, EventArgs e)

   {

 

     // 384 bits to 16384 bits in increments of 8 bits

     // Create Public & Private Key and store..

     RSACryptoServiceProvider RSAC = new RSACryptoServiceProvider(392);

     publickey = RSAC.ExportParameters(false);

     privatekey = RSAC.ExportParameters(true);

  

   }

 

   private void menuItem_Enc_Click(object sender, EventArgs e)

   {

       RSACryptoServiceProvider rs = new RSACryptoServiceProvider();

       rs.ImportParameters(publickey);

       encMsg = rs.Encrypt(Encoding.ASCII.GetBytes(txttoEnc.Text), false);

       txtEnc.Text = Encoding.ASCII.GetString(encMsg, 0, encMsg.Length);

   }

 

   private void menuItem_Decode_Click(object sender, EventArgs e)

   {

      byte[] decMsg;

      RSACryptoServiceProvider rs = new RSACryptoServiceProvider();

      rs.ImportParameters(privatekey);

      decMsg = rs.Decrypt(encMsg, false);

      txtDec.Text =Encoding.ASCII.GetString(decMsg, 0, decMsg.Length);

   }

}

Sunday, August 9, 2009

out Parameter in C#

out parameter is a very good option when need somthing give back from a method rather than return

using out keyword we can access the given out parameter value.

Wednesday, August 5, 2009

How to Use delegate in side c#.net

Simple we can say delegate is a variable which holds methods instead of members. We are writing an application which will display either “Good Morning” or “Good Night” according to value entered. (0-12:- “Good Morning” otherwise “Good night”)

        delegate string ReturnString(); // Declare this in top





How to Write a your own EventHander to a Class

This is example shows how you can write an own event to your class. I tried several methods and ways to do this even though this includes some basic logic behind.

We are going to write Car class which raise, invoke or fire an event when 2 drivers are getting in to the car.


 

Following shows our interface to add drivers to the car.


The following code describes how to handle the event. Code shows how to display message when two drivers are added to the car.

 

Monday, August 3, 2009

How to get Sum across the row in SQL

SELECT        Column1 + Column2 AS Expr1
FROM            Table1

But if you want to gwt the sum accross the column you can use SUM function available in the SQL.