string filename=""; 
  
if(myFile.PostedFile.ContentLength>0)
{
  string NM=myFile.PostedFile.FileName;
  int i=NM.LastIndexOf(".");
  string extNM=NM.Substring(i);
  filename="QC"+mid+extNM;
  UploadFile(filename);
}
else
{
  filename=attachmentLb.Text;//這個
}......SqlParameter YeWuAttachment=new SqlParameter("@YeWuAttachment",SqlDbType.VarChar);
YeWuAttachment.Value=(filename==string.Empty?DBNull.Value:filename);
......為什麼上面代碼會出錯,怎樣修正?謝謝大家!錯誤:
CSSModify.cs(233,56): error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'

解决方案 »

  1.   

    YeWuAttachment.Value=(filename==string.Empty?DBNull.Value:filename);
    ==>YeWuAttachment.Value=(filename==string.Empty?DBNull.Value:(object)filename);
      

  2.   

    哦,原來是這樣!不過又出現了:CSSHandle.cs(1007,6): error CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
      

  3.   

    try:if( filename == String.Empty)
    {
    YeWuAttachment.Value= DBNull.value;
    }else
    {
    YeWuAttachment.Value= filename;}
      

  4.   

    分開來是可以的。
    但就簡化了那樣寫就是不行,搞不明白!因此才有上面一問!
    那是什麼原因呢?
    ==============
    1。 ? A:B.  A和B的类型必须一致。
     我开始把 B的类型改成了object ,这句通过了,但与 参数的Varchar类型又不一致了。所以还是分开写。