從DB

2014-02-19 54 views
0
public EmployeeProfile(int EmpID) 
{ 
     if (byteArrayToImage(Emp.Photo.ToArray()) != null) 
      { 
       pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray()); 
       pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage; 
      } 
} 
public Image byteArrayToImage(Byte[] byteArrayIn) 
{ 
     MemoryStream ms = new MemoryStream(byteArrayIn); 
     Image returnImage = Image.FromStream(ms); 
     return returnImage; 
} 

顯示在圖片框照片我有這樣的代碼時,員工沒有一個畫面就會出現一條消息:enter image description here從DB

+0

請提及您想要做的事情,並顯示您的整個代碼,以給您適當的答案。 – AymenDaoudi

回答

2

看起來Emp.Photonull並呼籲ToArray()是給你空參考例外。試試這個:

if (Emp.Photo != null) 
{ 
    pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray()); 
    pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage; 
} 
0

看來你Emp原本沒有照片,這意味着Emp.Photo爲空,這就是爲什麼你有它的消息是「對象未設置方式的NullReferenceException對象」,你首先需要檢查,如果你的Emp對象有照片與否的實例:

public EmployeeProfile(int EmpID) 
     { 
       if (Emp.Photo != null) 
        { 
         pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray()); 
         pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage; 
        } 
     } 
0

確保的Emp照片不是空:

public EmployeeProfile(int EmpID) 
{ 
     if (Emp != null && Emp.Photo != null) 
     { 
      pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray()); 
      pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage; 
     } 
}