Solved

String to SQL Image Data Type

Posted on 2010-09-01
10
913 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

738 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