Click here to Skip to main content
15,887,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to pass Null value or empty bytes for image field in sql

What I have tried:

My Code is below
If Not pic Is Nothing Then
              imageConverter = New ImageConverter()
              imageByte = DirectCast(imageConverter.ConvertTo(pic, GetType(Byte())), Byte())
              objCmd.Parameters.AddWithValue("@EmpPhoto", imageByte)
          Else
              objCmd.Parameters.AddWithValue("@EmpPhoto", DBNull.Value)
          End If

It shows an Error
Operand type is incompatiable with the image
Posted
Updated 30-Jan-20 6:26am

Try:
objCmd.Parameters.AddWithValue("@EmpPhoto", SqlDbType.Image).Value = DBNull.Value

Interesting. That doesn't work...Nor does this:
try
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("INSERT INTO Table_4 (Photo) VALUES (@P)", con))
            {
            cmd.Parameters.AddWithValue("@P", SqlDbType.Image).Value = DBNull.Value;
            int result = cmd.ExecuteNonQuery();
            }
        }
    }
catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }

But this does:
try
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("INSERT INTO Table_4 (Photo) VALUES (@P)", con))
            {
            SqlParameter ip = new SqlParameter("@P", SqlDbType.Image);
            ip.Value = DBNull.Value;
            cmd.Parameters.Add(ip);
            int result = cmd.ExecuteNonQuery();
            }
        }
    }
catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }


So ... in VB:
Try

    Using con As SqlConnection = New SqlConnection(strConnect)
        con.Open()

        Using cmd As SqlCommand = New SqlCommand("INSERT INTO Table_4 (Photo) VALUES (@P)", con)
            Dim ip As SqlParameter = New SqlParameter("@P", SqlDbType.Image)
            ip.Value = DBNull.Value
            cmd.Parameters.Add(ip)
            Dim result As Integer = cmd.ExecuteNonQuery()
        End Using
    End Using

Catch ex As Exception
    Console.WriteLine(ex.Message)
End Try
 
Share this answer
 
v2
Comments
[no name] 21-Sep-18 5:14am    
How to pass Empty byte in vb
OriginalGriff 21-Sep-18 5:27am    
Interesting - see updated solution.
[no name] 21-Sep-18 5:52am    
Thank u very much sir Vb.net Coding is now working
OriginalGriff 21-Sep-18 5:56am    
You're welcome!
I've written it up as a tip for posterity:
https://www.codeproject.com/Tips/1260972/Passing-DbNull-Value-to-an-SQL-Image-column
[no name] 21-Sep-18 6:01am    
I already Refered this solution in code project But that time i'd made some mistake but so its not working now its working fine
Very Simple Solution


C# Text

C#
query = "insert into Customer (CustomerCode,LdegerCode,CustomerPicture) values ('0001','9999',NULL)"



Sql query Text

SQL
insert into Customer (CustomerCode,LdegerCode,CustomerPicture) values ('0001','9999',NULL)
 
Share this answer
 
Comments
Richard Deeming 30-Jan-20 12:29pm    
So your "solution" to this already-solved question is to introduce a SQL Injection[^] vulnerability that didn't exist in the original code?

Very bad idea.

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