2010-08-22 92 views
0

我需要的時候我DONOT上傳圖片。我想這樣做來插入NULL值,但我發現插入表。請任何一個幫助我插入Null值在DB

protected void BtnSubmit_Click(object sender, EventArgs e) 
{ 
     int RowAffected = 0; 
     byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength]; 
     HttpPostedFile uploadedImage = FileUpload1.PostedFile; 
     uploadedImage.InputStream.Read 
       (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength); 


     using (SqlConnection con = Connection.GetConnection()) 
     { 
      SqlCommand Com = new SqlCommand("Insert_IntoModel", con); 
      Com.CommandType = CommandType.StoredProcedure; 

      SqlParameter UploadedImage = new SqlParameter 
           ("@Image", SqlDbType.VarBinary, imageSize.Length); 
      UploadedImage.Value = imageSize; 
      Com.Parameters.Add(UploadedImage); 
      if (FU2.PostedFile == null && FU2.PostedFile.FileName == "") 
      { 

       Com.Parameters.Add("@Logo", SqlDbType.VarBinary).Value = DBNull.Value; 
      } 
      else 
      { 
       byte[] imageSize2 = new byte[FU2.PostedFile.ContentLength]; 
       HttpPostedFile uploadedImage2 = FU2.PostedFile; 
       uploadedImage2.InputStream.Read 
          (imageSize2, 0, (int)FU2.PostedFile.ContentLength); 

       SqlParameter UploadedImage2 = new SqlParameter 
           ("@Logo", SqlDbType.VarBinary, imageSize2.Length); 
       UploadedImage2.Value = imageSize2; 
       Com.Parameters.Add(UploadedImage2); 
      } 
      RowAffected = Com.ExecuteNonQuery(); 
      if (RowAffected > 0) 
      { 
       LblResult.Visible = true; 
       LblResult.Text = "Successfully Proccess"; 
       Tbl.Visible = false; 
      } 
     } 

    } 
} 
+0

你有什麼錯誤嗎?如果(FU2.PostedFile.ContentLength == 0){Com.Parameters.Add(「@ Logo」,SqlDbType.VarBinary).Value = System.DBNull.Value} – SLaks 2010-08-22 12:31:06

+0

沒有錯誤發生 – Myworld 2010-08-22 12:48:39

回答

3

嘗試:

if(FU2.PostedFile.ContentLength == 0) 
{ 
    SqlParameter UploadedImage2 = new SqlParameter("@Logo", SqlDbType.VarBinary, System.DBNull.Value); 
    Com.Parameters.Add(UploadedImage2); 
} 
+0

謝謝我修改後的代碼, – Myworld 2010-08-22 12:53:55