Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

managed code advantages and disadvantages

In this article I will try to explain you using managed code advantages and disadvantages.

Managed Code

Program, code written within your application domain (i.e. .Net Framework) is, managed code. The program that are within domain are faster.

In Detail:

The code which is written in .net Framework supported language called as managed code.  This code is directly executed by CLR with help of managed code execution. Any program that is written in .NET Framework is managed code.

Managed code uses CLR which to handle memory management, security, exception etc.

Advantages using Managed Code:

1.  Memory management
2.  Thread management
3.  Exception handling
4.  Garbage collection
5.  Security
6.  Base Library for code reusability.
7.  Able to write code in multiple languages.
8.  Performance improvement.
9. Type safety and security.   
10.  Reusability of Code

Disadvantages:

1.  New and changing technology it takes time for people to learn that technology
2.  Multi-platform support isn't available from MS and isn't available straight after installing Visual Studio, but with VS 2015 supporting Multi-platform.
3.   Managed code can be slower than native code
4.  NET framework is free to download but Code Editor is costly.
5.  Not suitable for High End Application

Managed Code example:

Following C# code is managed code
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
 
namespace C4RandDWebApp
{
   public class ProductDataAccess
   {
      private string conString = "Data Source=.\\;Integrated Security=true;Initial Catalog=WebShop";
 
      /// <summary>
      /// Get product Details with dataset
      /// </summary>
      Public DataSet GetProductDetails()
      {
         var con = new SqlConnection(conString);
         var da = new SqlDataAdapter("GetProductDetails", con)
         {
            SelectCommand =
            {
               CommandType = CommandType.StoredProcedure
            }
         };
         var ds = new DataSet();
         da.Fill(ds, "Products");
         return ds;
      }
}

Handling Null column values in sqlDataReader

Handling Null column values in sqlDataReader

In this article I will explain how to handle null values from data reader columns.

As .net developer/programmer you faced null reference exception issue with sqlDataReader columns frequently.I will explain some simple tricks that Handling Null column values with sqlDataReader.

What is SqlDataReader?

SqlDataReader Object provides a connection oriented data access to the SQL Server data Sources from C# applications.

Best way to check for null values:

Handling Null column values using turnery operator:

In this example I used turnery operator with DBNull.Value to check sql data reader columns value is null or not, if found null then column value assigned to some default value.

Example:

  public static
List<UserHistoryDeatils> UserHistoryData()
        {
            List<UserHistoryDeatils> returnList = null;
            using (var oConn = Database.Connect())
            {
                var sqlCmd= new SqlCommand();
                sqlCmd.Connection= oConn;
                sqlCmd.CommandType= CommandType.StoredProcedure;
 
                if (oConn.State !=
                    ConnectionState.Open)
                {
                    oConn.Open();
                }
                sqlCmd.CommandText= "UserHistory_SP";
                var reader = sqlCmd.ExecuteReader();                 if (reader.HasRows)
                {
                    returnList = new List<UserHistoryDeatils>();
                    while(reader.Read())
                    {
                        returnList.Add(
new UserHistoryDeatils((reader["Name"] != DBNull.Value)
? Convert.ToString(reader["Name"].ToString()) : string.Empty,
   (reader["Phone"]!= DBNull.Value)
? int.Parse(reader["Phone"].ToString()) : 0,
                                    //handling null value with null able fields
                                     (reader["Version"] != DBNull.Value)                                    ? int.Parse(reader["Version"].ToString()): (int?)null,
                                   (reader["LanguageID"] != DBNull.Value)
                             ? new UserLanguage { Id = int.Parse(reader["LanguageID"].ToString()) }
                                    : null,//handling null value with object
                                    (reader["Date"] != DBNull.Value)
                                    ? DateTime.Parse(reader["Date"].ToString())
                                    : DateTime.MinValue                            
                                    ));   } } reader.Close(); }
return returnList; }


Handling Null column values using simple common custom method:

Example:

using (var rdr =
dbcommand.ExecuteReader())
                    {
                        while(rdr.Read())
                        {
                            var UserData = new UserDetail();
 
                            if (HasColumn(rdr, "Name"))
                            {
                                if (rdr["Name"]!=DBNull.Value)
                                {
                                    UserData.FullName = rdr.GetString(rdr.GetOrdinal("Name"));                               }
                            } } }


Custom method:

   public bool HasColumn(IDataReader DataReader, string ColumnName)
        {
            try
            {
                return DataReader.GetOrdinal(ColumnName) >= 0;
            }
            catch(IndexOutOfRangeException)
            {
                //decides whether a specific column name exists or not.
                return false;
            }
        }