0

I am saving photo in SQL (varbinary(MAX) Data Type) with this code:

                    if (SPV1 == true)
                    {
                        imgVC1 = Image.FromFile(Open1.FileName);
                        imgFormat1 = picVisit1.BackgroundImage.RawFormat;
                        Ms1 = new MemoryStream();
                        imgVC1.Save(Ms1, imgFormat1);
                        byte[] ArrayV1 = Ms1.GetBuffer();

                        csCompanies.VisitCard1 = ArrayV1;
                    }
                    else                                                
                        csCompanies.VisitCard1 = null;

and it continues with this code in Class:

            if(VisitCard1==null)
                com.Parameters.AddWithValue("@VisitCard1", Convert.ToByte(VisitCard1));
            else
                com.Parameters.AddWithValue("@VisitCard1", VisitCard1);

I Used "If" and "Else" for saving "Null" Value, when user not changed default photo.

Null Data saved as "0x00" in SQL.

When I want show data, i want know that the Data in SQL is Null or not, if is Null, do something, and if not, do something !

But i can't compare Data of SQL with Null value! and when i am using

if(cscompanies.Logo1==Null)

result always is (False) [that mean its not Null, Even when it Saved as Null (0x00)

2 Answers 2

1

Don't confuse c# null with sql server null. They are different things.

Instead of saving c# null in the database, save DBNull.Value:

if(VisitCard1==null)
    com.Parameters.Add("@VisitCard1", SqlDbType.VarBinary, -1).Value = DBNull.Value;
else
   com.Parameters.Add("@VisitCard1", SqlDbType.VarBinary, -1).Value = VisitCard1;
Sign up to request clarification or add additional context in comments.

14 Comments

I Used (DBNull.value) insted of (Null) but i meet this error (( Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.))
Thanks 4 Ur Answer. Ur code was good 4 Null Value and solved my Problem <3 But when i used Ur code 4 saving Photo , when i wanted to show, i meet this Error ((Buffer cannot be null.))........ Did U Understand me? :D
This Part: com.Parameters.Add("@VisitCard1", SqlDbType.VarBinary, -1).Value = VisitCard1; but when i saved with my old code --> ((com.Parameters.AddWithValue("@VisitCard2", VisitCard2);)) it it was shown without Error
I've edited my answer once again. Added the missing Convert.ToByte.
Im so sory. Ur Previous Cod Was Correct :D --> com.Parameters.Add("@VisitCard1", SqlDbType.VarBinary, -1).Value = VisitCard1;
|
0

For Visit Image i used this plan:

in class :

if ((dt.Rows[0]["visitcard1"]) != DBNull.Value)
                    VisitCard1 = (byte[])dt.Rows[0]["VisitCard1"];
                else
                    VisitCard1 = null;

and in Form:

 if (csCompanies.Catalog5 != null)
                {
                    byte[] Array = csCompanies.Catalog5;
                    MS = new MemoryStream(Array);
                    picCata5.BackgroundImage = Image.FromStream(MS);
                }

Comments

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.