Sending an SMS message using .NET CF on Win Mobile

My best man asked me a few months ago to create him an app that would allow him to send a predefined sms to a predefined number on a click of a button using his Win Mobile phone. I thought about it and decided to write this application for him.

First thing that we need to do is to create a Smart Device project, and choose device application from the next page. In my solution i have one form called Sender and a Program.cs file, this is only the basics so we will not need anything more.

As you can see from the previous picture, the user interface is very intuitive and plain. We have two textboxes and a button that sends the SMS. This part of the application is completely working and can be used to send SMS from your Win Mobile phone.

For sending the SMS message we need only the code below, and of course the call from the Send button

using Microsoft.WindowsMobile.PocketOutlook;
public static void SendSms(string recipientNumber, string messageText)
{
try
{
var sms = new SmsMessage()
{
Body = messageText
};
sms.To.Add(new Recipient(recipientNumber));
sms.Send();
MessageBox.Show("Message sent!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

To fire up the sending from a command line(that was the original idea if you remember, he wanted to be able to send it on a click of a button) we need to handle some command line arguments. My idea was to call the program the same way we have called the method that send the message first the recipient number, and then the message. calling the program without arguments would open the form, any other number of arguments will show a message with the correct way of doing it.

[MTAThread]
static void Main(string[] args)
{
switch (args.Length)
{
case 0:
Application.Run(new Sender());
break;
case 2:
Sender.SendSms(args[0],args[1]);
break;
default:
MessageBox.Show("Wrong number of arguments entered. Correct order is RecipientNumber \"SmsText in quotes\"");
break;
}
}

We can also do a regular expression check depending on the numbers we allow the user to send the SMS to, if we want to limit the user in any way, because i know my only user, i believe i can trust him 🙂

Next thing you need is to set up a shortcut that calls the application using the parameters and bind the shortcut to a button on your device. Quick way to pay tram fare when the conductor enters the tram and you just forgot to buy the ticket that day.

Creating ASP.NET MVC application against Oracle database pt. 1

This is a first in a series of posts, as i will try to build ASP.NET MVC application against Oracle XE database, but this example can be used for any Oracle database greater than 9i.

I will try to use Oracle ODAC.net the latest version that i can get from the Oracle Site.

With a little help from LILkillaBEE i created generic helpers that return IEnumerable<T> on which i can use LINQ and play with it.

First thing first, as i am creating a Contact manager application part by part, the first functionality i want to implement is getting the data from the Oracle XE database and working with it in MVC way. So i have created the table with personal data only in it.

CREATE TABLE  PEOPLE
   (
    PERSON_ID NUMBER NOT NULL ENABLE,
    NAME VARCHAR2(100) NOT NULL  ENABLE,
    SURNAME VARCHAR2(100) NOT NULL ENABLE,
    ADDRESS_LINE1  VARCHAR2(100) NOT  NULL ENABLE,
    ADDRESS_LINE2 VARCHAR2(100),
    ZIP_CODE  VARCHAR2(100) NOT  NULL ENABLE,
    CITY VARCHAR2(100) NOT NULL  ENABLE,
    COUNTRY VARCHAR2(100) NOT NULL ENABLE,
     CONSTRAINT PEOPLE_PK PRIMARY KEY  (PERSON_ID) ENABLE
   );

CREATE OR REPLACE TRIGGER  BI_PEOPLE
  before insert on PEOPLE
  for  each row
begin
    select PEOPLE_SEQ.nextval into  :NEW.PERSON_ID from  dual;
end;

ALTER TRIGGER  BI_PEOPLE ENABLE;

After this, i have created the Person class in my project

public class Person
{
    public virtual int PersonID { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string AddressLine1 { get; set; }
    public virtual string AddressLine2 { get; set; }
    public virtual string ZipCode { get; set; }
    public virtual string City { get; set; }
    public virtual string Country { get; set; }
}

And an interface that i will implement later in my repository

interface IPersonInterface
{
    IEnumerable&lt;Person&gt; GetAll();
    Person GetById(int id);
    void Add(Person person);
    void Delete(int id);
}

EDIT: After considering to finish this tutorial i finally gave up because there is no easy way of doing this and i kept hitting the wall a few times.

How to get directory contents in C#

I am creating a backup application and have to get the list of files in some directory here is the simple way to do it. If you only need contents of one directory you can use this code, i output it to System.Console just as an example, you can catch everything in string[] or whatever collection you prefer for your application. You only need using System.IO for this example:

static void GetDirContents(string sDir)
       {
           try
            {
        foreach (string d in Directory.GetDirectories(sDir))
             {
                DirectoryInfo dirInfo = new DirectoryInfo(d);
                  Console.WriteLine("[DIR] " + dirInfo.Name);
                  foreach (string f in Directory.GetFiles(d))
                    {
                         FileInfo fInfo = new FileInfo(f);
                     Console.WriteLine("\t" + fInfo.Name);
                    }
              }
         }
             catch (Exception sex)
              {
                 Console.WriteLine(sex.Message);
             }
         }

If you are in need to do this recursively for all sub directories you just call the void after file writing for loop

static void GetDirContents(string sDir)
         {
             try
            {
                 foreach (string d in Directory.GetDirectories(sDir))
               {
                   DirectoryInfo dirInfo = new DirectoryInfo(d);
                     Console.WriteLine("[DIR] " + dirInfo.Name);
                     foreach (string f in Directory.GetFiles(d))
                 {
                    FileInfo fInfo = new FileInfo(f);
                    Console.WriteLine("\t" + fInfo.Name);
                   }
                    GetDirContents(d);
                }
           }
           catch (Exception sex)
          {
              Console.WriteLine(sex.Message);
          }
         }

See you soon with next example.