Search code examples
c#asp.netashx

string causes "reference not set to instance of object"


I have this code, which is reading data from a databse, then assigns a url from the web.config file to a string.

bool isRequestCI = false;
            if (context.Request.QueryString["RequestCI"] != null)
            {
                isRequestCI = Convert.ToBoolean(context.Request.QueryString["RequestCI"].ToString());
            }
            string GetFileInfo;
            if (isRequestCI)
            {
                GetFileInfo = "select [FileName],FileExtension,MD5Hash from ElementCIBinaryAttachments where CIBinaryFileID = " + binaryFileID;
            }
            else
                GetFileInfo = "select [FileName],FileExtension,MD5Hash from CaseFileBinaryAttachments where BinaryFileID = " + binaryFileID;
            SqlCommand comGetFileInfo = new SqlCommand(GetFileInfo, cnGetFile);
            SqlDataReader drFileInfo;
            cnGetFile.Open();
            drFileInfo = comGetFileInfo.ExecuteReader();
            string fileName = "";
            string fileExtension = "";
            string MD5Hash = "";
            while (drFileInfo.Read())
            {
                fileName = drFileInfo["FileName"].ToString().Trim();
                fileExtension = drFileInfo["FileExtension"].ToString().Trim();
                MD5Hash = drFileInfo["MD5Hash"].ToString().Trim();
            }
            drFileInfo.Close();
            cnGetFile.Close();

            string MD5PathRequestCI = ConfigurationManager.AppSettings["CIUploadedFiles"].ToString() + MD5Hash.Substring(0, 3) + @"\" + MD5Hash.Substring(3, 3) + @"\" + MD5Hash.Substring(6, 3) + @"\" + MD5Hash.Substring(9);

however, it is causing an exception at string MD5PathRequestCI saying "reference not set to an instance of an object and I have no clue why. I never used to do that or am I missing something?


Solution

  • ConfigurationManager.AppSettings["CIUploadedFiles"] is returning null. Make sure the value "CIUploadedFiles" exists in your config file.