Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want When there is no Image is selected then Automaticly it Save Null Value to SQL database . I'm Using C#

What I have tried:

if (EmpolyPicpictureBox.Image != null)
           {
               //use to save image
               MemoryStream ms = new MemoryStream();
               EmpolyPicpictureBox.Image.Save(ms, EmpolyPicpictureBox.Image.RawFormat);
               byte[] iemimg = ms.GetBuffer();
               ms.Close();
               cmd.Parameters.AddWithValue("@Emimage", iemimg);

           }
           else
           {
               cmd.Parameters.AddWithValue("@Emimage", EmpolyPicpictureBox.Image = null);
           }
Posted
Updated 17-Mar-18 21:48pm

1 solution

Use DBNull.Value Field (System)[^]
else
{
    cmd.Parameters.AddWithValue("@Emimage", DBNull.Value);
}
 
Share this answer
 
Comments
Fahid Zahoor 18-Mar-18 11:52am    
System.Data.SqlClient.SqlException: 'Operand type clash: nvarchar is incompatible with image'
This Error is Occur when i using DBNull.Value
OriginalGriff 18-Mar-18 12:09pm    
Use the debugger to look at exactly what you are passing, and what the SQL command looks like.
Richard Deeming 19-Mar-18 12:56pm    
cmd.Parameters.Add("@Emimage", SqlDbType.VarBinary).Value = DBNull.Value;

AddWithValue can't guess the type correctly if the value you give it is null.
Fahid Zahoor 19-Mar-18 13:48pm    
Thanks It Will Runing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900