2012-12-26 70 views
0
System.InvalidCastException: [A]System.Collections.Generic.List`1[UploadImages] cannot be cast to [B]System.Collections.Generic.List`1[UploadImages]. 

    if (ViewState["CurrentList"] != null) 
      { 
     ObjUpload.AddRange((List<UploadImages>)ViewState["CurrentList"]); // Getting the above error 
      } 

UploadImages ObjUp = new UploadImages();System.InvalidCastException in

List<UploadImages> ObjUpload = new List<UploadImages>(); 

    ObjUp.AlternateText = TxtAlternatetext.Text; 
    if (TxtFre.Text != "") 
    { 

     ObjUp.frequency = Convert.ToInt16(TxtFre.Text); 
    } 
    ObjUp.ImageURL = FileUpload1.PostedFile.FileName; 

    ObjUp.URL = TxtUrlToNavigate.Text; 
    ObjUp.ID = i; 

    ObjUpload.Add(ObjUp); 

我已經作出了類[Serializable接口]

此代碼的工作,但有時不工作有時

回答

3

使用as操作,以確保:

List<UploadImages> uploadImages = ViewState["CurrentList"] as List<UploadImages>; 

if(uploadImages != null) 
{ 
    ObjUpload.AddRange(uploadImages); 
} 
+0

謝謝: )... – vini

+0

不客氣。 – Azodious

相關問題