Saturday, February 25, 2012

#8 : Utilizing Outlook to send email in the .Net way

Microsoft Outlook has been widely used to send and receive emails.  But there are times the we need to connect our system to send information directly via email. Though it is possible also to send it directly without passing to Outlook, some users prefer it to send via Outlook so that they can monitor their emails easily.

Using Microsoft.Office.Interop, we can make a program that will connect to any MS Office application. This time, we will use it to send email directly from .Net application.

Here's the simple code on how to work with Outlook application;

        Dim oEmail As Outlook.MailItem
          Dim msoApp as New Outlook.Application
          oEmail = DirectCast( msoApp .CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
          With oEmail
            .To = "emailaddress@email.com"
            .CC = "CCemailaddress@email.com "
            .Subject = "Your Mail Title"
            .BodyFormat = Outlook.OlBodyFormat.olFormatPlain
            .Body = "Your email content"
            .Importance = Outlook.OlImportance.olImportanceHigh
            .ReadReceiptRequested = True
            Dim sAttachedmentPath As String = "C:\Attachments\sample.jpg"           
            .Attachments.Add( sAttachedmentPath  , Outlook.OlAttachmentType.olByValue)
            .Recipients.ResolveAll()
            .Save()
            .Display()
        End With


You can use it on button event or as a function to call on to send email. However, before running the code, be sure you already setup your Outlook to send and receive emails. Also, you need to add the Microsoft.Office.Interop in the Solution reference folder of your project.

Monday, February 6, 2012

#7: Silverlight 4 WCF RIA Part 2 (add, edit and delete)

This is a continuation of my previous blog about Silverlight WCF RIA. This time we will add, edit and delete records in our database.

Assuming that we already created our data entity model and the domain class controller as discussed in my previous blog. Let's create a layout for our entry form with a datagrid bind in our domain context and the add, edit, and delete buttons as shown;



To ADD new record;

Just simply type in corresponding data on each text box then double click on the add button. In the code behind, type the following code;


    templatetbl2 newTemplate = new templatetbl2
            {
                TimeOff = Convert.ToDateTime(txbDate.Text),
                Cause = txbCause.Text,
                Duration = Convert.ToDecimal(txbDuration.Text),
                Customer = Convert.ToDecimal(txbCustomer.Text),
                TotalOutrage = Convert.ToDecimal(txbTotal.Text),
                UtilityName = txbUtility.Text        
            };
            var ctx = new DomainService1();
            ctx.templatetbl2s.Add(newTemplate);
            ctx.SubmitChanges(createop =>
            {
                Uri target = new Uri("/Page4 ", UriKind.Relative);
                NavigationService.Navigate(target);
            }, null);
            
            MessageBox.Show("Successfully saved records!");


To Edit record;

You can just simply click on the selected record to edit. You can bind the data in the entry text box or you can just directly edit the selected cell of the datagrid. In this example, we will just select the the particular cell in the datagrid. After editing the cell contents, double click on the edit button and add the following code;

 templatetbl2DomainDataSource.SubmitChanges();

The SubmitChanges command will automatically updates the database base on the edited values of the selected datagrid cells.

To Delete record;

Select the row to be deleted and double click on the delete button. In the code behind, type the following codes;


         templatetbl2 ent = (templatetbl2)templatetbl2DomainDataSource.DataView.CurrentItem;
            DomainService1 dc = templatetbl2DomainDataSource.DomainContext as DomainService1;


            dc.templatetbl2s.Remove(ent);
            templatetbl2DomainDataSource.SubmitChanges();


You need to call the SubmitChanges command so that the delete method will be executed in the database. Calling the Remove(entityname) will only mark the data for deletion.

These are a very simple lines of codes but very useful codes to start a Silverlight project.

Happy coding!