[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String to SQL Image Data Type

Posted on 2010-09-01
10
Medium Priority
?
959 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
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
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

829 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