Blog ( Of Ryan Olshan)

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

VB.NET

Some people may not know that .NET 2.0 provides APIs for accessing everything in a configuration file. The most common question I see relates to accessing the mail settings in the system.net node programmatically.

C#

using System.Configuration;

using System.Web.Configuration;

using System.Net.Configuration;

 

Configuration configurationFile = WebConfigurationManager .OpenWebConfiguration( "PathToConfigFile" );

 

MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup( "system.net/mailSettings" ) as MailSettingsSectionGroup ;

 

if (mailSettings != null )

{

    int port = mailSettings.Smtp.Network.Port;

    string host = mailSettings.Smtp.Network.Host;

    string password = mailSettings.Smtp.Network.Password;

    string username = mailSettings.Smtp.Network.UserName;

}

VB.NET

Imports System.Configuration

Imports System.Web.Configuration

Imports System.Net.Configuration

 

Dim configurationFile As Configuration = WebConfigurationManager.OpenWebConfiguration( "PathToConfigFile" )

 

Dim mailSettings As MailSettingsSectionGroup = configurationFile.GetSectionGroup( "system.net/mailSettings" )

 

If Not mailSettings Is Nothing Then

    Dim port As Integer = mailSettings.Smtp.Network.Port

    Dim host As String = mailSettings.Smtp.Network.Host

    Dim password As String = mailSettings.Smtp.Network.Password

    Dim username As String = mailSettings.Smtp.Network.UserName

End If

Share this post: | | | | |
Posted: by Ryan Olshan | with
Filed under: , ,

Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.

Download C# System.Net.Mail Helper
Download VB.NET System.Net.Mail Helper

Calling the function from code

MailHelper .SendMailMessage( "" , "" , "" , "" , "Sample Subject" , "Sample body of text for mail message" )

MailHelper.cs

using System.Net.Mail;

 

public class MailHelper

{

   ///

   /// Sends an mail message

   ///

   /// Sender address

   /// Recepient address

   /// Bcc recepient

   /// Cc recepient

   /// Subject of mail message

   /// Body of mail message

   public static void SendMailMessage( string from, string to, string bcc, string cc, string subject, string body)

   {

      // Instantiate a new instance of MailMessage

      MailMessage mailMessage = new MailMessage ();

 

      // Set the sender address of the mail message

      mailMessage.From = new MailAddress (from);

      // Set the recepient address of the mail message

      mailMessage.To.Add( new MailAddress (to));

 

      // Check if the bcc value is null or an empty string

      if (! string .IsNullOrEmpty(bcc))

      {

        // Set the Bcc address of the mail message

        mailMessage.Bcc.Add( new MailAddress (bcc));

      }

 

      // Check if the cc value is null or an empty value

      if (! string .IsNullOrEmpty(cc))

      {

        // Set the CC address of the mail message

        mailMessage.CC.Add( new MailAddress (cc));

      }

 

      // Set the subject of the mail message

      mailMessage.Subject = subject;

      // Set the body of the mail message

      mailMessage.Body = body;

 

      // Set the format of the mail message body as HTML

      mailMessage.IsBodyHtml = true ;

      // Set the priority of the mail message to normal

      mailMessage.Priority = MailPriority .Normal;

 

      // Instantiate a new instance of SmtpClient

      SmtpClient smtpClient = new SmtpClient ();

      // Send the mail message

      smtpClient.Send(mailMessage);

   }

}

MailHelper.vb

Imports System.Net.Mail

 

Public Class MailHelper

    '''

    ''' Sends an mail message

    '''

    ''' Sender address

    ''' Recepient address

    ''' Bcc recepient

    ''' Cc recepient

    ''' Subject of mail message

    ''' Body of mail message

    Public Shared Sub SendMailMessage( ByVal from As String , ByVal recepient As String , ByVal bcc As String , ByVal cc As String , ByVal subject As String , ByVal body As String )

        ' Instantiate a new instance of MailMessage

        Dim mailMessage As New MailMessage()

 

        ' Set the sender address of the mail message

        mailMessage.From = New MailAddress(from)

        ' Set the recepient address of the mail message

        mailMessage.To.Add( New MailAddress(recepient))

        ' Check if the bcc value is nothing or an empty string

 

        If Not String .IsNullOrEmpty(bcc) Then

            ' Set the Bcc address of the mail message

            mailMessage.Bcc.Add( New MailAddress(bcc))

        End If

 

        ' Check if the cc value is nothing or an empty value

        If Not String .IsNullOrEmpty(cc) Then

            ' Set the CC address of the mail message

            mailMessage.CC.Add( New MailAddress(cc))

        End If

 

        ' Set the subject of the mail message

        mailMessage.Subject = subject

        ' Set the body of the mail message

        mailMessage.Body = body

 

        ' Set the format of the mail message body as HTML

        mailMessage.IsBodyHtml = True

        ' Set the priority of the mail message to normal

        mailMessage.Priority = MailPriority.Normal

 

        ' Instantiate a new instance of SmtpClient

        Dim smtpClient As New SmtpClient()

        ' Send the mail message

        smtpClient.Send(mailMessage)

    End Sub

End Class

Web.config

xml version = " 1.0 " ?>

< configuration >

  < system.net >

    < mailSettings >

      < smtp from = " " >

        < network host = " smtp.yourdomain.com " port = " 25 " userName = " yourUserName " password = " yourPassword " />

      smtp >

    mailSettings >

  system.net >

configuration >

Share this post: | | | | |
Posted: by Ryan Olshan | with
Filed under: ,
VB.NET 2.0 XML Comments

.NET 2.0 introduces XML comments to VB.NET 2.0. An XML comment starts with ''' (3 single quotes). While this is new to VB.NET 2.0, XML comments have been a supported feature of C# since version 1.0. On a side note, C# XML comments start with ///.

'''
''' I'm a VB.NET XML comment
'''

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Dec 25 2005, 09:48 PM by Ryan Olshan | with no comments
Filed under:
HttpContext Helper

Need to access an HttpContext object in a class, but can't because the class doesn't inherit System.Web.UI.Page? Here is a simple helper class that will allow you to access some of the most common HttpContext objects.

C#
using System;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;

///


///
Encapsulates all HTTP-specific information about an individual HTTP request.
///
public class HttpContextHelper
{
   ///
   /// Gets the System.Web.HttpApplicationState object for the current HTTP request.
   ///
   public static HttpApplicationState CurrentApplication
   {
      get { return HttpContext .Current.Application; }
   }

   ///
   /// Gets the System.Web.Caching.Cache object for the current HTTP request.
   ///
   public static Cache CurrentCache
   {
      get { return HttpContext .Current.Cache; }
   }

   ///
   /// Gets the System.Web.HttpRequest object for the current HTTP request.
   ///
   public static HttpRequest CurrentRequest
   {
      get { return HttpContext .Current.Request; }
   }

   ///
   /// Gets the System.Web.HttpResponse object for the current HTTP response.
   ///
   public static HttpResponse CurrentResponse
   {
      get { return HttpContext .Current.Response; }
   }

   ///
   /// Gets the System.Web.HttpServerUtility object that provides methods used in
   /// processing Web requests.
   ///
   public static HttpServerUtility CurrentServer
   {
      get { return HttpContext .Current.Server; }
   }

   ///
   /// Gets the System.Web.SessionState.HttpSessionState object for the current
   /// HTTP request.
   ///
   public static HttpSessionState CurrentSession
   {
      get { return HttpContext .Current.Session; }
   }
}

VB.NET
Imports System
Imports System.Web
Imports System.Web.Caching
Imports System.Web.SessionState

'''


''' Encapsulates all HTTP-specific information about an individual HTTP request.
'''

Public
Class HttpContextHelper
   '''
   ''' Gets the System.Web.HttpApplicationState object for the current HTTP request.
   '''

   Public Shared ReadOnly Property CurrentApplication() As HttpApplicationState
      Get
         Return HttpContext.Current.Application
      End Get
   End Property

    '''
   ''' Gets the System.Web.Caching.Cache object for the current HTTP request.
   '''

   Public
Shared ReadOnly Property CurrentCache() As
Cache
      Get
         Return HttpContext.Current.Cache
      End Get
   End Property

    '''
   ''' Gets the System.Web.HttpRequest object for the current HTTP request.
   '''
   
   Public
Shared ReadOnly Property CurrentRequest() As
HttpRequest
      Get
         Return HttpContext.Current.Request
      End Get
   End Property

    '''
   ''' Gets the System.Web.HttpResponse object for the current HTTP response.
   '''

   
Public Shared ReadOnly Property CurrentResponse() As HttpResponse
      Get
         Return HttpContext.Current.Response
      End Get
   End Property

    '''
   ''' Gets the System.Web.HttpServerUtility object that provides methods used in
    ''' processing Web requests.
   '''

   Public Shared ReadOnly Property CurrentServer() As HttpServerUtility
      Get
         Return HttpContext.Current.Server
      End Get
   End Property

    '''
   ''' Gets the System.Web.SessionState.HttpSessionState object for the current
    ''' HTTP request.
   '''

   
Public Shared ReadOnly Property CurrentSession() As HttpSessionState
      Get
         Return HttpContext.Current.Session
      End Get
   End Property
End
Class
Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Dec 25 2005, 03:44 PM by Ryan Olshan | with no comments
Filed under: , ,
Common DropDownList Issue

A common DropDownList issue I've seen on many forums and lists is about a DropDownList populating items 2x or not being able to get the selected value on postback when binding to a data source. This is due to the DropDownList having data bound to it again on postback before the form is processed. To prevent this, you will need to ensure that the DropDownList is only bound to data on the initial load of the page.

VB.NET
Private Sub Page_Load( ByVal sender As Object , ByVal e As EventArgs)
    If Not IsPostBack Then
       ' Code here to bind data to DropDownList
    End If
End Sub

C#
private void Page_Load( object sender, EventArgs e)
{
    if (!IsPostBack)
   {
       // Code here to bind data to DropDownList
   }
}

R

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Dec 17 2005, 07:20 PM by Ryan Olshan | with 1 comment(s)
Filed under: , ,
The Future Of Visual Basic
What does the next version of Visual Basic (9.0) have to offer? Microsoft has an overview of VB 9.0 at http://url123.com/mdgb5 .

R

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Dec 07 2005, 04:31 PM by Ryan Olshan | with no comments
Filed under:
Using Statement Article
Here is my latest article in which I discuss the use of the using statement in C# and VB.NET.

Using Statement Article

R
Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Dec 07 2005, 10:17 AM by Ryan Olshan | with no comments
Filed under: ,
What's wrong with Eval()?
The .NET Framwork 2.0 introduces Eval() . Eval() is a shortcut for Container.DataItem() . Since Eval() uses Reflection, it causes overhead. From a optimization standpoint, it is better to use Container.DataItem() .

VB.NET

Container.DataItem()

C#
DataReader: ((System.Data.Common.DbDataRecord)Container.DataItem)[]
DataSet: ((System.Data.DataRowView)Container.DataItem)[]

R
Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Dec 06 2005, 03:45 PM by Ryan Olshan | with 1 comment(s)
Filed under: , , ,
More Posts