How to save Telerik RadBinaryImage image to SQL Server database

3.6k Views Asked by At

I have a Telerik RadBinaryImage control which is displaying an image loaded using Telerik RadAsyncUpload.

I have an SQL table containing a 'Photo' column (image data type). I want to save to the database the image from the Telerik RadBinaryImage to the insert function which look like this:

Private Sub InsertPhotoIntoDB()
        Dim sMyConn As String = My.Settings.appDBConnString
        Dim myConnection As SqlConnection
        Dim myCommand As New SqlCommand
        myConnection = New SqlConnection(sMyConn)

        myConnection.Open()

        myCommand = New SqlCommand("INSERT INTO Photos(Photo) VALUES(@Photo)")
        myCommand.Connection = myConnection

        myCommand.Parameters.Add("@Photo", SqlDbType.Image, 0, "Photo")
        myCommand.Parameters("@Photo").Value = WhatDoIPutHere???

        myCommand.ExecuteNonQuery()

        myConnection.Close()
        myConnection.Dispose()
End Sub

I've tried: myCommand.Parameters.Add("@Photo", SqlDbType.Image).Value = RadBinaryImage1.DataValue but I still get an error:

The parameterized query '(@Photo image)INSERT INTO Photos (Photo) VALUES (@Photo)' expects parameter '@Photo', which was not supplied.

Do I need to convert RadBinaryImage1.DataValue to image?

2

There are 2 best solutions below

2
On

The safest way is to upload the image as a byte array:

Byte[] yourByteArray; 
...
myCommand.Parameters.AddWithValue("@Photo", yourByteArray);

How to get to a byte array differs. With the ASP.NET Upload control, it's just:

yourByteArray = FileUpload1.FileBytes;

Or if the image is represented as a stream:

var yourByteArray = new Byte[ImageObject.InputStream.Length];
ImageObject.InputStream.Read(yourByteArray, 0, yourByteArray.Length);
2
On

The RadBinaryImage control is used to display an image coming from the database. So, the sequence should be like this:

  1. User uploads photo via RadUpload
  2. Image stored in database using modified code (see below)
  3. On PostBack, the page displays the image from the database using the RadBinaryImage

If you don't want to store in the database right away, you will need to persist the data yourself in some way (Could store the file on the filesystem and then access the path via RadBinaryImage.SavedImageName, or persist in memory as Session["UploadedImage"]). The bottom line is RadBinaryImage is only meant to display an image on the page.

With regards to your sample code, I recommend using the SQL column type varbinary instead of image.

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
         Handles Button1.Click
        For Each file As UploadedFile In RadUpload1.UploadedFiles
            Dim bytes(file.ContentLength - 1) As Byte
            file.InputStream.Read(bytes, 0, file.ContentLength)
            Dim connection As OleDbConnection = CreateConnection()
            Try
                Dim command As New OleDbCommand("INSERT INTO Images ([Name], [Size], [Content]) VALUES (?, ?, ?)", connection)
                command.Parameters.AddWithValue("@Name", file.GetName())
                command.Parameters.AddWithValue("@Size", bytes.Length)
                command.Parameters.AddWithValue("@Content", bytes)
                connection.Open()
                command.ExecuteNonQuery()
            Finally
                If connection.State = Data.ConnectionState.Open Then
                    connection.Close()
                End If
            End Try
        Next
    End Sub

See this post for sample code and more explanation: http://www.telerik.com/help/aspnet-ajax/upload-manipulating-files.html