开发者

Insert Null value In DB

I need to insert NULL value when I donot upload image .I tried to do that but I find insert in table .Please any one help me

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)F开发者_如何学CileUpload1.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;
            }
        }

    }
}


Try:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜