Search code examples
c#asp.netviewbagargumentnullexception

Global DropDown list allways null


I am having struggles loading the data of drop downs when loading the page. I somehow always get an error that my object is null. When I debug it seems like the error in the PrepareViewBag() function raises before even executing the code in Globals.cs to acctually fill up the drop down lists. I dont know why the code behaves like this, but in my opinion it should at least come to the PopulateDropDownList() function in Globals.cs before giving out an ArgumentNullException. Any suggestions?

Calling the View:

public ActionResult Index()
{
    PreprareViewBag();
    return View();
}

Prepare ViewBag (exception):

public class BaseController : Controller 
{
    protected void PreprareViewBag()
    {
        ViewBag.DropDownListExtension = new SelectList(Globals.Constants.DropDownListExtension, "Value", "Text");
        ViewBag.DropDownListDeveloper = new SelectList(Globals.Constants.DropDownListDeveloper, "Value", "Text");
        ViewBag.DropDownListSchema = new SelectList(Globals.Constants.DropDownListSchema, "Value", "Text");
        ViewBag.DropDownListRelease = new SelectList(Globals.Constants.DropDownListRelease, "Value", "Text");
        ViewBag.DropDownListBuild = new SelectList(Globals.Constants.DropDownListBuild, "Value", "Text");
        ViewBag.DropDownListStatus = new SelectList(Globals.Constants.DropDownListStatus, "Value", "Text");
        ViewBag.DropDownListDeploymentRelease = new SelectList(Globals.Constants.DropDownListDeploymentRelease, "Value", "Text");
    }
}

An exception of type 'System.ArgumentNullException' occurred in System.Web.Mvc.dll but was not handled in user code

Additional information: Value cannot be null.

Globals.cs to fill the dropdowns:

public class Globals : Controller
{
    // GET: Globals
    public class Constants
    {
        public static String ConnectionString;

        //ADM Portal specific stuff
        public static List<SelectListItem> DropDownListRelease;
        public static List<SelectListItem> DropDownListBuild;
        public static List<SelectListItem> DropDownListSchema;
        public static List<SelectListItem> DropDownListStatus;
        public static List<SelectListItem> DropDownListDeveloper;
        public static List<SelectListItem> DropDownListExtension;
        public static List<SelectListItem> DropDownListDeploymentRelease;   //deployable releases




        public Constants()
        {      
            PopulateDropDownList(ref DropDownListRelease, "ADM_PORTAL.P_LISTRELEASES");
            PopulateDropDownList(ref DropDownListBuild, "ADM_PORTAL.P_LISTBUILDS");
            PopulateDropDownList(ref DropDownListSchema, "ADM_PORTAL.P_LISTSCHEMAS");
            PopulateDropDownList(ref DropDownListStatus, "ADM_PORTAL.P_LISTSTATUSES");
            PopulateDropDownList(ref DropDownListDeveloper, "ADM_PORTAL.P_LISTDEVELOPERS");
            PopulateDropDownList(ref DropDownListExtension, "ADM_PORTAL.P_LISTFILEEXTENSIONS");
            PopulateDropDownList(ref DropDownListDeploymentRelease, "ADM_PORTAL.P_LISTDEPLOYMENTRELEASES");
        }

        /// <summary>
        /// Populates drop down list from the database.
        /// </summary>
        /// <param name="destinationList">populates only if list is null</param>
        /// <param name="oracleCommand">e.g.: ADM_PORTAL.P_LISTFILEEXTENSIONS</param>
        /// <returns>number of items in the list</returns>
        public static int PopulateDropDownList(ref List<SelectListItem> destinationList, string oracleCommand)
        {
            ConnectionString = "User Id=adm_owner; password=/Bpvc7bm_wenYz#@; Data Source=chdbd1_genone.ch.glencore.net;";

            OracleParameter[] oracleParameters = new OracleParameter[2];
            oracleParameters[0] = new OracleParameter("pc_recordset", OracleDbType.RefCursor, ParameterDirection.Output);
            oracleParameters[1] = new OracleParameter("pn_includecanceled", OracleDbType.Long, 30, 0, ParameterDirection.Input);
            return PopulateDropDownList(ref destinationList, oracleCommand, oracleParameters);
        }

        public static int PopulateDropDownList(ref List<SelectListItem> destinationList, string oracleCommand, OracleParameter[] oracleParameters)
        {
            int i = 0;

            if (destinationList == null)
            {
                destinationList = new List<SelectListItem>();
                using (OracleConnection con = new OracleConnection(ConnectionString))
                {
                    con.Open();
                    OracleCommand cmd = new OracleCommand(oracleCommand, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.BindByName = true;
                    for (int p = 0; p < oracleParameters.Length; cmd.Parameters.Add(oracleParameters[p]), p++) ;

                    // Execute command
                    OracleDataReader reader;
                    try
                    {
                        reader = cmd.ExecuteReader();
                        if (!reader.HasRows)
                        {
                            //nothing on the list
                        }
                        else
                        {
                            //build list
                            while (reader.Read())
                            {
                                SelectListItem sli = new SelectListItem();
                                sli.Value = reader.GetString(1);        //DATAVALUE
                                sli.Text = reader.GetString(0);         //DISPLAYVALUE
                                destinationList.Add(sli);
                                i++;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // log error...
                    }
                }
            }
            else
            {
                i = destinationList.Count;
            }

            return i;
        }

    }
}

Many Thanks


Solution

  • The fields you refer to are static, but your constructor isn't. Therefore the code in the constructor doesn't run before you access a static property.

    Make the constructor static instead, see MSDN: Static Constructors (C# Programming Guide):

    Change

    public Constants()
    

    To

    static Constants()