2011-11-21 2 views
0

약간의 질문이 있습니다. 내 파일을 업로드하기 위해 blob을 사용하고 있으며, 예를 들어 영화 나 mp3 일 때도 다운로드 할 수 있습니다. 그러나 사진을 다운로드하려고 할 때 항상 가지고 있습니다. 이 예외는 ... 누군가 왜 저에게 말할 수 있습니까?System.Byte, 업로드 그림

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'. 

Description: An unhandled exception occurred during the execution of the current 
web request. Please review the stack trace for more information about the error 
and where it originated in the code. 

Exception Details: System.InvalidCastException: Unable to cast object of type 
'System.DBNull' to type 'System.Byte[]'. 

Source Error: 

An unhandled exception was generated during the execution of the current web requt. 
Information regarding the origin and location of the exception can be identified using 
the exception stack trace below. 

도움 주셔서 감사합니다. 그리고

This is the code to retrieve them . 

     string filename = Request["file"].ToString(); 
     var conString = ConfigurationManager.ConnectionStrings["LocalSqlServer"]; 
     string strConnString = conString.ConnectionString; 
     SqlConnection dbConnection = new SqlConnection(strConnString); 
     dynamic queryString = ("SELECT Data,ContentType FROM Files WHERE Name = '" + filename + "'"); 
     SqlCommand theCommand = new SqlCommand(queryString, dbConnection); 
     dbConnection.Open(); 
     SqlDataReader reader = theCommand.ExecuteReader(); 

     if (reader.Read() && reader != null) 
     { 

      Byte[] bytes; 
      bytes = Encoding.UTF8.GetBytes(String.Empty); 
      bytes = (Byte[])reader["Data"]; 
          string contentType = reader["ContentType"].ToString(); 
      Response.ContentType = contentType; 
      Response.AddHeader("content-disposition", "attachment; filename=" + filename + ""); 
      Response.BinaryWrite(bytes); 
      reader.Close(); 
     } 

     dbConnection.Close(); 

그들을 업로드 : 그것은 당신이 검색을 시도하는 방울 같은 소리

dbConnection.Open(); 
         string queryString = "INSERT INTO Files (Name,Path,UserUpload,Date,Data,ContentType,Size) VALUES (@Name,@Path,@UserUpload,@Date,@Data,@ContentType,@Size);" + "SELECT CAST(scope_identity() AS int)"; 
         SqlCommand theCommand = new SqlCommand(queryString, dbConnection); 
         theCommand.Parameters.AddWithValue("@Name", FileUpload1.FileName); 
         theCommand.Parameters.AddWithValue("@Path", GetTheCurrentDirectory(MyTreeView.SelectedNode)); 
         theCommand.Parameters.AddWithValue("@UserUpload", Request.Cookies["UserSettings"]["UserName"]); 
         theCommand.Parameters.AddWithValue("@Date", DateTime.Now); 
         theCommand.Parameters.AddWithValue("@Data", FileUpload1.FileBytes); 
         theCommand.Parameters.AddWithValue("@ContentType", FileUpload1.PostedFile.ContentType); 
         theCommand.Parameters.AddWithValue("@Size", FileUpload1.PostedFile.ContentLength); 

         int newFid = (Int32)theCommand.ExecuteScalar(); 

         dynamic queryStringFolder = "INSERT INTO FILES_FOLDERS (File_Id,Folder_Id) VALUES (@FileId,@FolderId);"; 
         theCommand = new SqlCommand(queryStringFolder, dbConnection); 
         theCommand.Parameters.AddWithValue("@FileId", newFid); 
         theCommand.Parameters.AddWithValue("@FolderId", MyTreeView.SelectedValue); 
         theCommand.ExecuteNonQuery(); 

답변

1

은 NULL입니다. 예상 된 조건 인 경우 바이트 배열로 캐스트하기 전에 DBNull에 대한 확인을해야합니다.

+0

하지만 내 데이터베이스에서 바이트를 볼 수 있기 때문에 이상합니다. = ( – Kiwimoisi

+0

시나리오에 대한 자세한 정보를 제공하십시오. 데이터베이스에서 데이터를 가져 오는 방법은 무엇입니까? –

+0

중단 점을 배치하면 DB에서 데이터를 가져온 직후의 데이터 바이트? – sq33G

관련 문제