My byte[] becomes empty when inserted to database

Last post 07-05-2009 2:09 PM by Naom. 4 replies.

Sort Posts:

  • My byte[] becomes empty when inserted to database

    07-04-2009, 6:38 PM
    • Member
      260 point Member
    • Zcumbag
    • Member since 04-18-2004, 1:16 PM
    • Posts 98

    Hello.

    I have a procedure that (is supposed to) instert a byte[] into a varbinary-field in sql server.

    Here's the code:

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
                SqlCommand cmd = new SqlCommand("SP_InsertStuff", conn);
                cmd.CommandType = CommandType.StoredProcedure;
    
                cmd.Parameters.Add("@Id", SqlDbType.UniqueIdentifier);
                cmd.Parameters["@Id"].Value = pId; // pId is a Guid
    
                cmd.Parameters.Add("@datetime", SqlDbType.DateTime);
                cmd.Parameters["@datetime"].Value = datetime;
    
                cmd.Parameters.Add("@bindata", SqlDbType.VarBinary);
                cmd.Parameters["@ bindata"].Value = bindata; // bindata is a byte[]
    
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    return true;
                }
                catch
                {
                    return false;
                }
                finally
                {
                    conn.Close();
                }


    When I put a breakpoint in the code I can clearly see that the bindata contains lots of information. Everything seems to be working nice. Except, when I retrieve the data, or just look in my table, the bindata-column is empty (or poosibly just 1 byte size).

    Why is this? Am I doing something wrong?


    /D


  • Re: My byte[] becomes empty when inserted to database

    07-04-2009, 8:10 PM
    • Contributor
      4,934 point Contributor
    • Segundo
    • Member since 09-07-2006, 2:44 PM
    • Lima, Perú
    • Posts 693

    Hi,

    Please

    1.- which's the data type of that column in your database table?

    2.- which's the data type you use to save that data in your code-beside?

    Any doubt, post your comment.

  • Re: My byte[] becomes empty when inserted to database

    07-05-2009, 7:13 AM
    • Member
      260 point Member
    • Zcumbag
    • Member since 04-18-2004, 1:16 PM
    • Posts 98

    The column is a varbinary(8000) in my sql server 2008.

    My bindata[] gets its value like this:

    BinaryFormatter bf = new BinaryFormatter();
                MemoryStream s = new MemoryStream();
                bf.Serialize(s, this);

    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream s = new MemoryStream();
    bf.Serialize(s, this); // "this" is a class 
    
    bindata = s.ToArray();
    // at this row I can see that the bindata contains lots of info
    




  • Re: My byte[] becomes empty when inserted to database

    07-05-2009, 1:23 PM
    Answer
    • Contributor
      5,222 point Contributor
    • RickNZ
    • Member since 01-01-2009, 8:43 AM
    • Nelson, New Zealand
    • Posts 863

    Try removing the space between the @ and bindata, on the line where you set its value.


  • Re: My byte[] becomes empty when inserted to database

    07-05-2009, 2:09 PM
    Answer
    • All-Star
      30,534 point All-Star
    • Naom
    • Member since 12-31-2007, 2:08 PM
    • Wisconsin
    • Posts 6,781

     In your original code

    cmd.Parameters["@ bindata"].Value = bindata; // bindata is a byte[]   // remove a space here

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
Page 1 of 1 (5 items)