Queens Dev
Some more ASP.NET

System.Net.Mail - Command Not Implemented

September 19, 2008 at 5:20 AM by bjsetegn

While setting up the e-mailing features int log4net for an ASP.NET 2.0 project I kept receiving the following exception:

System.Net.Mail.SmtpException: Command not implemented. 
 The server response was: Command not implemented
   at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at log4net.Appender.SmtpAppender.SendEmail(String messageBody)
   at log4net.Appender.SmtpAppender.SendBuffer(LoggingEvent[] events)

I found this troubling because I had used this feature before and had no problem sending e-mails from other applications.  Also another developer was able to use the same exact log4net code on their machine and it worked.  It turned out that the host I was using wasn't setup for relaying e-mails through their e-mail server.  When I used the .NET 1.1 System.Web.Mail classes with the same e-mail server and configuration I would not receive the exception.  It uses a slightly different communication protocol (HELO vs EHLO) that doesn't require relaying to be setup on the e-mail server. So if all else fails and you get the Command Not Implemented exception you could always use the .NET 1.1 System.Web.Mail class.

More Info

http://msexchangeteam.com/archive/2004/02/19/76432.aspx - For setting up an email server for relaying.

http://code.msdn.microsoft.com/KB913616 - For a possible hotfix.

Handle DB Null and Return Default in C#

May 9, 2008 at 2:21 AM by bjsetegn

 When retrieving data from a database you must handle null values.  The getDefaultIfDBNull method below will check if the value returned from the database is null.  If it is null then it will return the default value for that type of object.  For example if retrieving an int field from the database and passed a null value and TypeCode of Int32 it will return the default value of 0.  This helps streamline the checking for null proccess and ensures that all nulls are handled in a simliar way.

The Code:

        /// <summary>

        /// Checks if an object coming back from the database is dbnull.  If it is this returns the default

        /// value for that type of object.

        /// </summary>

        /// <param name="obj">Object to check for null.</param>

        /// <param name="typeCode">Type of object, used to determine what the default value is.</param>

        /// <returns>Either the object passed in or the default value.</returns>

        public static object getDefaultIfDBNull(object obj, TypeCode typeCode)

        {

            //If object is dbnull then return the default for that type.

            //Otherwise just return the orginal value.

            if (obj == DBNull.Value)

            {

                switch (typeCode)

                {

                    case TypeCode.Int32:

                        obj = 0;

                        break;

 

                    case TypeCode.Double:

                        obj = 0;

                        break;

 

                    case TypeCode.String:

                        obj = "";

                        break;

 

                    case TypeCode.Boolean:

                        obj = false;

                        break;

 

                    case TypeCode.DateTime:

                        obj = new DateTime();

                        break;

 

                    default:

                        break;

                }

            }

 

            return obj;

        }

 

This is a sample of a method that calls a stored procedure and needs to check for null.

 

        private void FillDetailInfo(int intDetailsId)

        {

            string conString = "";

            SqlDataReader dr;

 

            try

            {

                conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

                SqlConnection con = new SqlConnection(conString);

 

                SqlCommand cmd = new SqlCommand("uspGetDetails", con);

                cmd.CommandType = CommandType.StoredProcedure;

                cmd.CommandTimeout = 100000;

                cmd.Parameters.AddWithValue("@intDetailsId", intDetailsId);

 

                con.Open();

                dr = cmd.ExecuteReader();

 

                //Get details

                if (dr.Read())

                {

                    _id = (int)dr["Id"];

                    _desc = (string)SecurityTools.getDefaultIfDBNull(dr["desc"], TypeCode.String);

                    _isActive = (bool)SecurityTools.getDefaultIfDBNull(dr["ACTIVE"], TypeCode.Boolean);

                }

 

                con.Close();

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

End Code 

ASP.NET - Repeater vs Datalist vs Gridview

March 14, 2008 at 8:33 AM by bjsetegn

There's a whole lot to be said about the Repeater, Datalist, and Gridview controls and I think it best if they speak for themselves.  So below is a simple page with a repeater, datalist, and gridview.  Each control loads the same sample data to show how they each render.  You can download the code below to take a look at how they each get created.  There are best practices tips in there as well.  Asking what the difference is between these three controls is a great question in an interview with a junior level developer.  If they know when and how to use each they have a mastered the basics of ASP.NET.

Also when taking a look at the link below take a look at the page source.  A great tool for this if you don't have it yet is Firebug.  It will make developing and styling and ASP.NET page 10x easier.

Repeater, Datalist, and Gridview Sample Page:

http://www.queensdev.net/Samples/RepeaterDataListGridview.aspx

To run the sample simply extract the files in the zip to their own folder, open Visual Studio 2005 or 2008, go to File->Open->Website and open the folder you extracted the files to.

Sample code can be found here:

RepeaterDatalistGridview.zip

Tags: ,

ASP.NET | C#