February 2006 - Posts

A New Approach to Web Services - Today and Tomorrow

In a Web Services-based world, communication is based on public contracts - the technical and partly semantic description of a Web Service. The most common kind of contract, the technical interface definition, is today usually expressed in the form of WSDL. The possibility to automatically generate these interface documents allows developers to select either a code-first approach or contract-first approach for creating their contracts.

But schema-based contract-first design is in many cases the more interoperable approach as you define the service messages with the XML type system of the SOAP standard, not the type system of any given programming language or platform

Check out the link below for gory details:



Obi Oberoi



How to Send EMail from VS.Net using System.Web.Mail

It is not uncommon in a project to implement a functionality of sending out email from an ASP.Net page in the form of a Feedback or something similar.  Here's how you can leverage the Web.Mail class to build a simple yet powerful functionality:

Imports System.Web.Mail

Public Class SendEmail

  Private Sub cmdSendEmail_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles cmdSendEmail.Click

    Dim objMail As New MailMessage()

    objMail.From = Me.txtFrom.Text.ToString
    objMail.To = Me.txtTo.Text.ToString
    objMail.Subject = Me.txtSubject.Text.ToString
    objMail.Body = Me.txtMessage.Text.ToString
    objMail.BodyFormat = MailFormat.Html
    Me.lblMessage.Text = "Email has been sent"

  End Sub

End Class


Below is the code I wrote for a Windows app using Framework 1.1

 Public Sub SendEmail(ByVal sFromEmailAddress As String, ByVal sToEmailAddress As String, _
                                 ByVal sEmailSubject As String, ByVal sEmailBody As String)

      Dim objMail As New MailMessage

         SmtpMail.SmtpServer.Insert(0, "MY_SERVER")

         objMail.From = sFromEmailAddress
         objMail.To = sToEmailAddress
         objMail.Subject = sEmailSubject
         objMail.Body = sEmailBody
         objMail.BodyFormat = MailFormat.Text

         objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "0")

      Catch ex As Exception
         Throw ex.InnerException
      End Try

   End Sub


While the above is a plain vanilla code to do in framework 1.1.  However, to do it in 2.0 is little different.  Please see below:

It seems that the protocol for sending email in .NET 2.0 has changed from what it was in 1.1.  The System.Web.Mail namespace has been replaced with the new System.Net.Mail names code samplepace.  The following is a C# code sample on how to send mail with 2.0:

System.Net.Mail.SmtpClient o_Client = new System.Net.Mail.SmtpClient(this.MailServer);
o_FromAddress = new System.Net.Mail.MailAddress(this.FromEmail, this.FromName);
System.Net.Mail.MailAddress o_ToAddress = new System.Net.Mail.MailAddress(this.ToEmail, this
System.Net.Mail.MailMessage o_Message = new System.Net.Mail.MailMessage
(o_FromAddress, o_ToAddress);
o_Message.Subject = this
o_Message.Body = this

The properties contained in “this“ should be obvious. As you can see, we now get a “friendly” send name so we no longer have to rig it as we did in 1.1.

VB.Net Version in .Net 2.0

Private Sub SendEmail(ByVal MsgFrom As String)

Dim objMM As New MailMessage(MsgFrom, "ToAddress@mail.com")

'Create the SmtpClient object
Dim smtp As New SmtpClient

'Assign the MailMessage's properties
objMM.Subject = txtSubject.Text

objMM.Body = txtBody.Text
objMM.IsBodyHtml = False
objMM.CC.Add(CELL_PHONE) 'Constant value
objMM.CC.Add(WEB_MASTER) 'Constant value

'Send the MailMessage

End Sub


Obi Oberoi

Launching Excel file from VB.Net

In my current project I was required to read the entire data from a DataGridView (VS.Net 2005) and dump it onto an Excel spreadsheet.  Here's how:

Before you start working with Excel, add an Excel reference to your project by selecting 'Microsoft Excel 9.0 Object Library' or later.

The following code simply iterates through the rows and columns of a DataGridView, adds the contents to a StringBuilder object and then writes to an excel file and finally opens the excel file.

    Dim iRow, iCol As Int16
    Dim value As String
    Dim col0 As String = String.Empty
    Dim col1 As String = String.Empty
    Dim sbHeader As New Text.StringBuilder
    Dim sbMain As New Text.StringBuilder

            sbHeader.Append("ID," & vbTab & "Expense")

            For iRow = 0 To grdCustomer.RowCount - 1
                For iCol = 0 To grdCustomer.ColumnCount - 1
                    value = grdCustomer.Item(iCol, iRow).Value.ToString
                    Select Case iCol
                        Case 0
                            col0 = value
                        Case 1
                            col1 = value
                            sbMain.Append(col0 & vbTab & col1)
                    End Select

            Print("test.xls",sbHeader, sbMain)

        Catch ex As Exception
        End Try

    Friend Sub Print(ByVal fileName As String, ByVal sbHeader As StringBuilder, ByVal sbMain As StringBuilder)
            Dim dirName As String = DIRECTORY_NAME

            If Not Directory.Exists(dirName) Then
            End If

            Dim filePath As String = dirName & fileName

            System.IO.File.WriteAllText(filePath, sbHeader.Append(sbMain).ToString)
            xlBook = xlApp.Workbooks.Open(filePath)

            xlApp.Visible = True
        Catch ex As Exception
        End Try


Obi Oberoi

VB.Net Vs C#

I am sure by now you must have all read people ranting in newsgroups and/or forums on the future of VB.Net and it how it compares to C#...

Even though VB.Net is very close to my heart as this is the first language that I learnt to use professionally.  However, I am somewhat irresolute about it's future.

The reason being that seven job postings out of ten require C# as the primary language for Winforms and/or Web development.  I hate to sound pessimistic, but if

the trend continues as such and industry leaders continue to favor C# over VB.Net, then I am following suit.

If any one of you would like to share your experiences, please feel welcome to post your comments.


Obi Oberoi