9 Replies - 848 Views - Last Post: 10 November 2016 - 10:09 AM Rate Topic: -----

#1 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 20-May 16

How can i get binary value from database and convertinto image?

Posted 08 November 2016 - 11:23 AM

I have a sql database with column name = "Photo" and it's datatype is "Varbinary(max)"
So when I upload a picture in database it looks something like this: 0xFFD8FFE000104A46...


I want to display this photo using gridview:


 <asp:GridView ID="GridView1" runat="server" onrowediting="EditB" OnRowDataBound = "RowDataBound"
            DataKeyNames="ID"  ... >
		<Columns>
		...
			<asp:TemplateField HeaderText = "Photo">
				<ItemTemplate>
					<asp:Image ID="Photo" runat="server" Height="80" Width="80" />
				</ItemTemplate>
			</asp:TemplateField>
		...
		</Columns>
		...



This part i need help with
Error: bytes is not declared.
How can i get binary value from database and convertinto image?

Protected Sub RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView1.EditIndex = e.Row.RowIndex Then
            Dim bytes As Byte() = DirectCast(GetData(e.Rows.FindControl("Photo"), Byte())
            Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)
            TryCast(e.Row.FindControl("Photo"), Image).ImageUrl = Convert.ToString("data:image/png;base64,") & base64String
		End If
    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: How can i get binary value from database and convertinto image?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13656
  • View blog
  • Posts: 54,499
  • Joined: 12-June 08

Re: How can i get binary value from database and convertinto image?

Posted 08 November 2016 - 11:26 AM

Typically I find it better to _not_ store the binary in the database, but store the image in a file path and store the file path in the database. I would

If you run this, and just write the image out, does the image come through?
Was This Post Helpful? -1
  • +
  • -

#3 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 20-May 16

Re: How can i get binary value from database and convertinto image?

Posted 08 November 2016 - 11:31 AM

View Postmodi123_1, on 08 November 2016 - 11:26 AM, said:

Typically I find it better to _not_ store the binary in the database, but store the image in a file path and store the file path in the database. I would

If you run this, and just write the image out, does the image come through?


nope doesnt shows up. I will have user uploading a small pic. I just want to store it database and display in gridview.

I was also thinking I am getting select query from database. to display in gridview. so what if i some how convert binary into image in this method?

    Protected Sub BindGridData()
        ' Connect to databse and open database file
        Dim da As SqlDataAdapter
        Dim ds As DataSet = New DataSet()
        Dim query As String = "SELECT * FROM [table];"
        Dim sqlConn As New SqlConnection(myConnectionString)

        Try
            sqlConn.Open()

            Dim cmd = New SqlCommand(query, sqlConn)
            da = New SqlDataAdapter(cmd)
            Dim builder As SqlCommandBuilder = New SqlCommandBuilder(da)
            da.Fill(ds, "table")

            Dim a As Integer = ds.Tables("table").Rows.Count
            If a = 0 Then
                'empty
            End If
            GridView1.DataSource = ds.Tables("table").DefaultView
            GridView1.DataBind()

            sqlConn.Close()
        Catch ex As Exception
        End Try
    End Sub

This post has been edited by ikhlas06: 08 November 2016 - 11:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13656
  • View blog
  • Posts: 54,499
  • Joined: 12-June 08

Re: How can i get binary value from database and convertinto image?

Posted 08 November 2016 - 11:40 AM

Like I said a database is not a file system to store things in. Among other things, using a db to store whole files slows it down, can cause issues with database corruption, jacks with size/volume, etc.

You can use a memorystream to take in a byte array and 'image.from stream' to jump from that to an image object.
Was This Post Helpful? -1
  • +
  • -

#5 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 20-May 16

Re: How can i get binary value from database and convertinto image?

Posted 08 November 2016 - 11:56 AM

View Postmodi123_1, on 08 November 2016 - 11:40 AM, said:

Like I said a database is not a file system to store things in. Among other things, using a db to store whole files slows it down, can cause issues with database corruption, jacks with size/volume, etc.

You can use a memorystream to take in a byte array and 'image.from stream' to jump from that to an image object.



is there an example i can see?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13656
  • View blog
  • Posts: 54,499
  • Joined: 12-June 08

Re: How can i get binary value from database and convertinto image?

Posted 08 November 2016 - 11:59 AM

I don't follow.
Was This Post Helpful? -1
  • +
  • -

#7 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 20-May 16

Re: How can i get binary value from database and convertinto image?

Posted 08 November 2016 - 12:04 PM

View Postmodi123_1, on 08 November 2016 - 11:59 AM, said:

I don't follow.

it will help if i can see an similar example. i tierd to search on good but its not same as what i am trying to do
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13656
  • View blog
  • Posts: 54,499
  • Joined: 12-June 08

Re: How can i get binary value from database and convertinto image?

Posted 08 November 2016 - 12:44 PM

It's like three lines. I explained the steps in my post above.
Was This Post Helpful? -1
  • +
  • -

#9 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13656
  • View blog
  • Posts: 54,499
  • Joined: 12-June 08

Re: How can i get binary value from database and convertinto image?

Posted 10 November 2016 - 07:26 AM

Snap. All my posts down voted. Thanks!
Was This Post Helpful? 1
  • +
  • -

#10 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 20-May 16

Re: How can i get binary value from database and convertinto image?

Posted 10 November 2016 - 10:09 AM

i tried to remove neg votes but it is not letting me. sorry
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1