Solved

String to SQL Image Data Type

Posted on 2010-09-01
10
873 Views
Last Modified: 2012-05-10
I have a field in SQL that is saved as image data type I am able to pull this from the DB and convert it into a string in a textbox. But then I want to save the information back to the DB if the user makes changes and I can’t figure out how to convert a string to image data type.
0
Comment
Question by:ourguru
10 Comments
 
LVL 18

Expert Comment

by:x-men
ID: 33579023
maybe this MSDN artical will shed some light:

http://msdn.microsoft.com/en-us/library/ms187993.aspx

0
 

Author Comment

by:ourguru
ID: 33579226
Thank you for your comment i forgot to add that I am trying to send this from a textbox in visual basic and am using sql 2005
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 33581004
Try this:

    Public Function Base64ToImage(ByVal base64String As String) As Image

        ' Convert Base64 String to byte()

        Dim imageBytes As Byte() = Convert.FromBase64String(base64String)

        Dim ms As New System.IO.MemoryStream(imageBytes, 0, imageBytes.Length)



        ' Convert byte[] to Image

        ms.Write(imageBytes, 0, imageBytes.Length)

        Dim image1 As Image = Image.FromStream(ms, True)

        Return image1

    End Function

Open in new window

0
 

Author Comment

by:ourguru
ID: 33581149
I tried the function and it returned the error below

System.FormatException was unhandled
  Message=Invalid character in a Base-64 string.
  Source=mscorlib
 
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33581211
How do you convert the image to string?
Try this:




Public Function ImageToBase64(image As Image, format As System.Drawing.Imaging.ImageFormat) As String

Using ms As New MemoryStream()

' Convert Image to byte()

image.Save(ms, format)

Dim imageBytes As Byte() = ms.ToArray()



' Convert byte() to Base64 String

Dim base64String As String = Convert.ToBase64String(imageBytes)

Return base64String

End Using

End Function

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ourguru
ID: 33581262
Dim dBytes As Byte() ='this is where the select statement from my sql server is
        Dim str As String
        Dim enc As New System.Text.ASCIIEncoding()
        str = enc.GetString(dBytes)
        BITSTextBox.Text = str
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33581307
Try this:

  Dim dBytes As Byte() ='this is where the select statement from my sql server is
        Dim str As String
        str = Convert.ToBase64String(dBytes)
        BITSTextBox.Text = str                              
0
 

Author Comment

by:ourguru
ID: 33581394
that converted it but into a bunch of unreadable characters I need the users to be able to read what is there and edit/add to it
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33582875
And I have to ask and please do not take this the wrong way, but why in the world is the column defined as an image data type in the first place if you are using it as text.
0
 

Author Comment

by:ourguru
ID: 33617269
it is from our ERP I have no control on how the tables are setup
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now