0

I want to insert null value to the variable of image datatype and then update the column of image datatype in the Database

Dim PHOTO as Image = row("PHOTO")

Here, row("PHOTO") is fetched from a DataTable. If row("PHOTO") is Null, I want to insert the null value in PHOTO variable. I tried a lot but didn't succeed. Your help in this regard will be highly appreciated.

18
  • did you try: If IsNothing(row("PHOTO")) Commented Jun 14, 2016 at 9:47
  • Yes, it didn't work. The error popups "Unable to insert null value in System.Web.UI.WebControls.Image" Commented Jun 14, 2016 at 9:53
  • Not so sure about vb.net, but shouldn't row("PHOTO") be equal to DBNull.Value and not to Nothing? Commented Jun 14, 2016 at 10:00
  • OK, please try to use: IsDbNull() Commented Jun 14, 2016 at 10:03
  • Used that one too. As its datatype is Image, so it is not accepting any other parameter/ value Commented Jun 14, 2016 at 10:05

1 Answer 1

3

try following example,

If NOT IsDbNull(row("PHOTO")) Then
   //Do something
ELSE
   //your logic goes here
Dim PHOTO as Image=DBNull.Value;
End If

EDIT As you mentioned in comment, In C# my approach that works for me is...

 byte[] bimage = null;
if (txtPic.Text != "")
                {
                    string image = txtPic.Text;
                    Bitmap bmp = new Bitmap(image);
                    FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
                    bimage = new byte[fs.Length];
                    fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
                    fs.Close();
                }

and now for stored procedure my image parameter will be as

if (bimage != null)
                    cmd.Parameters.AddWithValue("@imgdata", SqlDbType.Image).Value = bimage;
                else
                {
                    SqlParameter imageParameter = new SqlParameter("@imgdata", SqlDbType.Image);
                    imageParameter.Value = DBNull.Value;
                    cmd.Parameters.Add(imageParameter);
                }
Sign up to request clarification or add additional context in comments.

2 Comments

On declaring the variable, the error popups: Value of type 'System.DBNull' cannot be converted to 'System.Web.UI.WebControls.Image''
This is because System.Web.UI.WebControls.Image can be used only with NULL value (and not with System.DBNull) when talking about nullable object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.