Popular Posts

Feb 23, 2011

Sending Mail (C#)


Take a windows form. Take a textbox from toolbox and change the name of the textbox. i change it to emailToTextBox. Another textbox for subject and chance to subjectFromTextBox.Another RichTextBox for body of the mail. bodyRichTextBox.Now take a button and write the code behind the button
[ smtp.Host = ""; //Like "smtp.gmail.com" ]


        private void sendMessageButton_Click(object sender, EventArgs e)
        {
            string to = emailToTextBox.Text;
            string subject = subjectFromTextBox.Text;
            string mailBody = bodyrRichTextBox.Text;

            if (to == "" && subject == "" && mailBody == "")
            {

            }
            else
            {
                MailMessage msg = new MailMessage();
                msg.To.Add(to);
                msg.From = new MailAddress(to);
                msg.Subject = subject;
                msg.Body = mailBody;

                msg.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "";  
                smtp.Credentials = new System.Net.NetworkCredential("x@y.com", "passxyz");
                smtp.EnableSsl = true;
                try
                {
                    smtp.Send(msg);
                    MessageBox.Show("Message Has Been Sent.");
                }
                catch
                {
                    MessageBox.Show("Message can't send...");
                }
            }

Feb 17, 2011

USING ENUM(C#)


In your professional or student life you have written down some sort of code snippet as follows:

Sample 1 (C# Code):

            string employeeType = employeeTypeComboBox.Text;
            if (employeeType == "Permanent")
            {
                CalculateSalary();
            }
            else if (employeeType == "Worker")
            {
                CalculateWages();
            }
            else
            {
                // Do nothing
            }

Sample 2 (C# Code):

            string studentType = studentTypeComboBox.Text;
            if (studentType == "Regular")
            {
                // Do Something
            }
            else if (studentType == "Private")
            {
                // Do Something
            }
            else
            {
                // Do nothing
            }


In these two samples, user input is taken in a string valriable (employeeType /studentType) from a ComboBox and compare the variable with some discrete hard-coded string values (Permanent/Worker/Regular/Private).

What will happen if you type (some of you has faced these sad experiences)  Parmanent instead of Permanent in Sample 2? Definitely CalculateSalary() method will be not called and even program doesn’t show you any error message (any compile error).  In best case, You will find out this problem during development and fix it immediately. But in worst case this problem arises after demployment L.

Is there any solution where my type will be detected earliar and show me a compile error?

Yes. Try emun.
See Sample Code 3, the more smart version of Sample code 1. Here you have no typo option J

Sample Code 3:

  enum EmployeeType
        {
            Permanent,
            Worker
        }

  string employeeType = employeeTypeComboBox.Text;
        if (employeeType == EmployeeType.Permanent.ToString())
        {
            CalculateSalary();
        }
        else if (employeeType == EmployeeType.Worker.ToString())
        {
            CalculateWages();
        }
        else
        {
             //Do Nothing
        }


So, why enum?
a)      To improve code clarity
b)      Make the code easier to maintain
c)      Getting error at earlier stage


Note:
a)      You can define enum inside or outside of class, but not inside of method or property.
b)      The list of names contained by a particutar type of enum called enumerator list.

More About enum:
a)      If you want you can keep value with each name in enumerator list. Example:

        enum Priority
        {
            Critical = 1,
            Important = 2,
Medium = 3,
Low = 4
  };

Also get this value by type casting to the related type.

int priorityValue = (int) Priority.Medium;

b)      Another nice thing, you can easily iterate through enumerator list.

foreach(Priority priority in Enum.GetValues(typeof(Priority)))
         {
             MessageBox.Show(priority.ToString());
   }

      or even

         for (Priority prio = Priority.Low; prio >= Priority.Critical; prio--)
         {
             MessageBox.Show(prio.ToString());
   }


Practice:

Write a program where a ComboBox contains days of a week. User will select a day and press Information Button.

If Saturday or Wednesday is selected (s)he will get the information: “Today you have a three hours long OOP class :-)”

If Sunday or Tueday is selected (s)he will get the information: “Today you have a six hours long OOP class :-)”

If Friday, Monday or Thursday is selected (s)he will get the information: “Today you have no OOP class :-(”



Feb 10, 2011

USING TRIGGER

Database triggers or triggers are SQL statements that are stored in the database catalog. Once triggers are activated by database events such as UPDATE, DELETE or INSERT, the triggers will execute either before or after the event that initiates them.In ORACLE how you can create triggers and use them...

create table employees
( id number primary key,
name varchar2(50));

create or replace trigger uppername
before insert or update on employees
for each row
begin
:new.name:=upper(:new.name);
end;
/

insert into employees values(1, 'Jubayer');

select * from employees;

create or replace trigger uppername
before insert or update on employees
for each row
begin
if inserting then
:new.name:=upper(:new.name);
elsif updating then
:new.name:=lower(:new.name);
end if;
end;
/

insert into employees values(2, 'khalid');

<<<<<<<<<<<<<<<<update>>>>>>>>>>>>>>>>>>>

create or replace trigger uppername
before insert or update on employees
for each row
begin
if inserting then
:new.name:=upper(:new.name);
elsif updating then
:new.name:=lower(:old.name);
end if;
end;
/

insert into employees values(3, 'Shoaib');

update employees set name='Amin' where id=3;



create table log(
log_data varchar2(50),
username varchar2(50),
timeofaccess date);

create or replace trigger uppername
before insert or update on employees
for each row
begin
if inserting then
:new.name:=upper(:new.name);
elsif updating then
insert into log values('updating', sys_context('userenv', 'session_user'), sysdate);
end if;
end;
/



create or replace trigger uppername
before insert or update on employees
for each row
begin
if inserting then
:new.name:=upper(:new.name);
elsif updating then
update employees set name='Sukarna' where id=1;
end if;
end;
/


create or replace trigger uppername
before insert or update on employees
begin
if updating then
update employees set name='Sukarna' where id=1;
end if;
end;
/


create or replace trigger uppername
before insert or update on employees
declare
temp_id number;
begin
if updating then
select max(id) into temp_id from employees;
insert into employees values(temp_id, 'Sayeed');
end if;
end;
/




create or replace trigger uppername
before insert or update on employees
declare
temp_id number;
begin
if updating then
select max(id) into temp_id from employees;
insert into employees values(temp_id+1, 'Sayeed');
end if;
end;
/


****************Can't
create or replace trigger id_gen
after insert on employees
for each row
declare
temp_id number;
begin
select max(id) into temp_id from employees;
:old.id:=temp_id+1;
end;
/


create or replace trigger id_gen
after insert on employees
for each row
declare
temp_id number;
begin
select max(id) into temp_id from employees;
:new.id:=temp_id+1;
end;
/

create or replace trigger id_gen
before insert on employees
for each row
declare
temp_id number;
begin
select max(id) into temp_id from employees;
:old.id:=temp_id+1;
end;
/


create or replace trigger id_gen
before insert on employees
for each row
declare
temp_id number;
begin
select max(id) into temp_id from employees;
:new.id:=temp_id+1;
end;
/

insert into employees values(12, 'Abu');


create or replace trigger id_gen
before insert on employees
for each row
declare
temp_id number;
begin
if :old.id is null then
select max(id) into temp_id from employees;
:new.id:=temp_id+1;
end if;
end;
/

create or replace trigger id_gen
before insert on employees
for each row
declare
temp_id number;
begin
if :new.id is null then
select max(id) into temp_id from employees;
:new.id:=temp_id+1;
end if;
end;
/



create or replace trigger after_create
after create on database
begin
insert into log values(ora_sysevent, sys_context('userenv', 'session_user'), sysdate);
end;
/


create or replace trigger logon_trigg
after logon on database
begin
insert into log values(ora_sysevent, sys_context('userenv', 'session_user'), sysdate);
end;
/

Feb 2, 2011

Using List<> in C#



Both the List<T> and ArrayList classes have properties very similar to C# arrays. One key advantage of these classes over arrays is that they can grow and shrink as the number of stored objects changes.
The List<T> class is contained with the System.Collections.Generic namespace whilst the ArrayList class is contained within theSystem.Collections namespace.
The syntax for creating a List<T> collection is as follows:
List<typename = new List<type>();


Step1: Make a new project. Select console application. named the project ListNamed.
Step2: Make a class Person.cs
Type the code
public class Person
    {
        public int age;
        public string name;

        public Person(int age, string name)
        {

            this.age = age;

            this.name = name;

        }

    }
Step3:In Program.cs file type the code like this…
List<Person> people = new List<Person>();

            people.Add(new Person(50, "Ahmed"));
            people.Add(new Person(30, "Saikat"));
            people.Add(new Person(26, "Rumon"));
            people.Add(new Person(24, "Jubayer"));
            people.Add(new Person(25, "Khalid"));
            people.Add(new Person(6, "Ismail"));


            Console.WriteLine("Unsorted list");

            people.ForEach(delegate(Person p)
            { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

            // Find the young
            List<Person> young = people.FindAll(delegate(Person p) { return p.age < 25; });
            Console.WriteLine("Age is less than 25");

            young.ForEach(delegate(Person p)
            { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

            // Sort by name
            Console.WriteLine("Sorted list, by name");
            people.Sort(delegate(Person p1, Person p2)
            { return p1.name.CompareTo(p2.name); });

            people.ForEach(delegate(Person p)
            { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

            // Sort by age
            Console.WriteLine("Sorted list, by age");

            people.Sort(delegate(Person p1, Person p2)
            { return p1.age.CompareTo(p2.age); });

            people.ForEach(delegate(Person p)
            { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

            Console.ReadLine();

Step4:Run the project. You will see the result…