2012-12-11 23 views
0

我有這段代碼,它正在從數據庫中讀取數據,然後將web.config文件中的url分配給一個字符串。字符串導致「引用未設置爲對象的實例」

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); 

然而,在string MD5PathRequestCI引起異常說「引用不設置到對象的實例,我不知道爲什麼,我從來沒有使用過這樣做,還是我失去了一些東西?

+1

這個值實際設置了嗎? 'ConfigurationManager.AppSettings [ 「CIUploadedFiles」]'。另外,出於調試目的,將它們中的每一個連接起來,而不是一次性地查看問題來自哪裏。 – Stephen

回答

2

ConfigurationManager.AppSettings["CIUploadedFiles"]將返回空值。確保值「CIUploadedFiles」存在於你的配置文件。

+0

啊謝謝你。我只是再次檢查,顯然「CIUploadedFiles」實際上是假設爲「CIUploadedFilesStorage」。我一定很困惑,並使用目錄名稱,而不是web.config密鑰名稱 – john

2

檢查web.config文件,確保您有在你的配置的部分關鍵「CIUploadedFiles」的的appSettings條目。本節在根元素內:

<configuration> 
<appSettings> 
    <add key="CIUploadedFiles" value=""/> 
</appSettings> 
</configuration> 
+0

謝謝。是的,這是問題,我很困惑,因爲我在查看目錄文件夾名稱而不是web.config密鑰名稱 – john

相關問題