Solved

String to SQL Image Data Type

Posted on 2010-09-01
10
907 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

740 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