Solved

String to SQL Image Data Type

Posted on 2010-09-01
10
922 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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
 

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

691 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