Wednesday, May 6, 2009

WORD,LINE,CHARACTER COUNTER IN C#


static void Main(string[] args)  {
  string str = "hello world\nmy name is sumair\n IV";
  int wordcount = 0,charcount = 0,linecount = 0;
  //calculating first line
  if (str != "")
  {
  linecount++;
  }
  for (int i = 0; i < 2; i++)
  {
  //calculating first and last word
  if ((str[0] >= 97 && str[0] <= 122) || (str[str.Length - 1] >= 97 && str[0] <= 122))
  {
  wordcount++;
  }
  }
  for (int i = 0; i < str.Length; i++)
  {
  if (str[i] == ' ')
  {
  wordcount++;
  }
  if ((str[i] >= 97 && str[i] <= 122) || (str[i] >= 65 && str[i] <= 91)) 
  {
  charcount++;
  }
  if (str[i] == '\n')
  {
  linecount++;
  }
  }
  Console.WriteLine("Total words are : "+wordcount+"\nTotal characters are : "+charcount+"\nTotal lines are : " + linecount);
  Console.ReadKey();
  }

Friday, April 17, 2009

EXTENSION METHODS

Extension methods allow developers to extends the functionality of an existing type without creating a new derived type.

Simple Extension Method Example:

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

namespace extmethods
{
  //This class Must be Public.
  // even sealed classes can extends by extension methods

  public sealed class human
  {
      public void eat()
    {
  Console.WriteLine("Eat Food");
   }
   
}
   
 ///


  /// This is my human Extensions class.This class must be static.
///

  public static class humanextension
  {
  /// This is our extension method for the human class.The first argument of a class extension should always be named "this" and should have the Type of the Type you want to extend.In this example class human is being Extend.Thats why argumentof the method contains this human objectname.This method must be static.
 
   
    public static void eatpkfood(this human h) 
   {
    Console.WriteLine("Eat Pak Food");
   }

}
  class Program
 {
  static void Main(string[] args)
    {
  human extmethod = new human();

 /// The following line used the "eatpkfood" extension method.You can see that object of human class acesses the method of humanextension  class.

 extmethod.eatpkfood();

//This line invokes the "eatpkfood" method of humanextension class.

humanextension.eatpkfood(extmethod);
 Console.Read();
    }
 }
}

OUTPUT:

Eat Pak Food

Eat Pak Food




ADO.NET (PART 7)


         HOW TO LOGIN THROUGH DATABASE 

DATABASE:
NAME = Northwind.

TABLE :
NAME = Login.

FIELDS:

username,password.


private void BtnLogin_Click(object sender, EventArgs e)
  {
  SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
  SqlDataAdapter da = new SqlDataAdapter("select * from login", cn);
  SqlCommand cmd = new SqlCommand();
  cmd.Connection= cn;
  cmd.CommandText = "select *from login where username = '" + txtUser.Text + " ' and password = '" + txtPwd.Text + "'";
  cmd.Connection.Open();
  SqlDataReader dr;
  dr=cmd.ExecuteReader();
  if(dr.Read())
  {
  Form1 f = new Form1();
  f.Show();
  this.Hide();
  }
  else
  {
  MessageBox.Show( "Invalid UserName or Password!!","invalid");
  }

}