hey everyone i need help with navigating images in a database, i was able to insert image to a database. btw, i am using sql server 2005 and visual studio 2008. i only converted a sample code of visual c to .net and i was able to do so, but the problem is, i cant move through images...

here's my code for inserting an image into a database:

constr = "Data Source=CARLSATELITE\SQLEXPRESS;Initial Catalog=walalang;User ID=sa;Password=sa"
        con = New SqlConnection(constr)

        'Dim ra As Integer
        'Dim fs As FileStream
        'Dim rawdata As Byte()
        fs = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
        rawdata = New Byte(Convert.ToInt32(fs.Length)) {}
        fs.Read(rawdata, 0, Convert.ToInt32(fs.Length))

            com = New SqlCommand("Insert into images (Image) values(@k)", con)
            com.Parameters.AddWithValue("@k", rawdata)
            ra = com.ExecuteNonQuery()
            MsgBox("" & ra)

        Catch ex As Exception
        End Try

        PictureBox1.Image = Image.FromStream(New MemoryStream(rawdata))

i also found a code for moving through records but i cant convert it to .net syntax because it is in c:

pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])ds.Tables[0].Rows[i].ItemArray[0]));

i am having a problem particularly on byte[]..

please help me again guys..tnx in advance

Recommended Answers

pictureBox1.Image = Image.FromStream(new MemoryStream(ds.Tables(0).Rows(i)(0))
Jump to Post

All 4 Replies

pictureBox1.Image = Image.FromStream(new MemoryStream(ds.Tables(0).Rows(i)(0))

hey adatapost, tnx for the code but i was having an error:

Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
'Public Sub New(buffer() As Byte)': Argument matching parameter 'buffer' narrows from 'Object' to '1-dimensional array of Byte'.
'Public Sub New(capacity As Integer)': Argument matching parameter 'capacity' narrows from 'Object' to 'Integer'.

i still dont know what to do

Use CType to unbox,

pictureBox1.Image = Image.FromStream(new MemoryStream(CType(ds.Tables(0).Rows(i)(0),Byte()))

tnx adatapost ur code worked well but i am still getting errors. i modified the code and what i did is

PictureBox1.Image = Image.FromStream(New MemoryStream(CType(ds.Tables("images").Rows(inc).Item("Image"), Byte())))

the code will work on the first two pictures, but then after that, it will prompt an error saying, "Parameter is invalid"

tnx in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.