Bigtingz92
Bigtingz92

Reputation: 129

Trying to convert a varbinary(MAX) value in SQL to byte[] and displaying in an asp image control

The end goal will be to be able to select a company and all its details from the database using a webform, then update/create new entries. All this works, except with the Logo

The Logo is stored in a SQL database as varbinary(MAX), which is have pulled into C# and converted into byte[] in order to display it using an asp image controller.

Front end

<img src="" runat="server" id ="image" />

Pulling the Logo from the Database using a SqlDataReader

        public TemplateData(SqlDataReader dr)
    {
        initialiseData();
        if (dr.HasRows)
        {

            byte[] Logo = (byte[])dr["Logo"];

            //Logo = dr["Logo"].ToString();
            TemplateId = dr["TemplateId"].ToString();
            Comment = dr["Comment"].ToString();
            SchemeCode = dr["SchemeCode"].ToString();
            Version = dr["Version"].ToString();
        }
    }

Code behind

        protected void ddSchemeCode_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddSchemeCode.SelectedIndex > 0)
        {
            // Existing Data to load from database
            TemplateData temp = DataClass.ReturnData(ddSchemeCode.SelectedItem.Text);
            if (temp != null)
            {
                txtVersion.Text = temp.Version;
                txtComment.Text = temp.Comment;
                txtSchemeCode.Text = temp.SchemeCode;
                txtTemplateId.Text = temp.TemplateId;
                image.Src = "data:image/jpeg;base64," + Convert.ToBase64String(temp.Logo);
            }

        }

The Error I receive is when a company is selected is "Value cannot be null. Parameter name: inArray"

I've tried umpteen other ways of doing this and none of them seem to do the trick. Any help is much appreciated, Cheers!

Upvotes: 2

Views: 1866

Answers (2)

image.Src = "data:image/jpeg;base64," + Convert.ToBase64String((byte[])dr["Logo"]);

Above code is working. We have to insert/update image to column Logo. Ex:

update <table> 
    set Logo= (SELECT BulkColumn 
    FROM Openrowset( Bulk 'LogoPath...' , Single_Blob) as img)

Upvotes: 0

Evan Mulawski
Evan Mulawski

Reputation: 55354

According to MSDN, inArray is the parameter name for Convert.ToBase64String(byte[]). Therefore, temp.Logo must be null.

Upvotes: 2

Related Questions