Click to See Complete Forum and Search --> : saving image


aamir55
November 4th, 2008, 05:52 AM
i have a program in which i store an album into the database using Album table and after it I store a picture in that album into the database. I use an other table for pictures called Photos.
I want to save photo after editing so i have added following code in button's click event. There is no error is comming but photo is not stored.


Dim str As New IO.MemoryStream

pb.Image.Save(str, Imaging.ImageFormat.Jpeg)
Dim buffer(CInt(str.Length - 1)) As Byte

' Read the bytes from this stream
str.Read(buffer, 0, CInt(str.Length))
' Now we can close the stream
Dim con As New SqlConnection(My.Settings.ConnectionString)
Dim cmd As New SqlCommand("INSERT INTO Photos Values(@name,@desc,@album_id,@photo)", con)
cmd.CommandType = CommandType.Text
Dim param(3) As SqlParameter
With param
param(0) = New SqlParameter()
param(0).ParameterName = "@name"
param(0).SqlDbType = SqlDbType.VarChar
param(0).Value = "name"
cmd.Parameters.Add(param(0))

param(1) = New SqlParameter()
param(1).ParameterName = "@desc"
param(1).SqlDbType = SqlDbType.VarChar
param(1).Value = "desc"
cmd.Parameters.Add(param(1))

param(2) = New SqlParameter()
param(2).ParameterName = "@album_id"
param(2).SqlDbType = SqlDbType.Int
param(2).Value = 1
cmd.Parameters.Add(param(2))

param(3) = New SqlParameter()
param(3).ParameterName = "@photo"
param(3).SqlDbType = SqlDbType.Image
param(3).Value = buffer
cmd.Parameters.Add(param(3))
End With
'cmd.Parameters.Add(param)
con.Open()
cmd.ExecuteNonQuery()
'str.Close()
End Sub


no error is comming now but picture is not stored in database
i also want to show u my database structure which is as follow

CREATE TABLE [dbo].[Albums] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[desc] [varchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Photos] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[desc] [varchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[album_id] [int] NOT NULL ,
[photo] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

CREATE PROC sp_GetPhotoAlbums AS
SELECT Albums.[id] AS AlbumID, Albums.[name] AS Album, Albums.[desc] AS Album_Desc,
Photos.[id] AS PhotoID, Photos.[name] AS Photo, Photos.photo, Photos.[desc] AS Photo_Desc
FROM Albums INNER JOIN
Photos ON Albums.[id] = Photos.album_id
ORDER BY Albums.[id]
GO

CREATE PROCEDURE sp_InsertPhoto
@name AS VARCHAR(50),
@image AS IMAGE,
@album AS INT
AS

INSERT INTO Photos ([name], photo, album_id)
VALUES (@name, @image, @album)

RETURN @@identity

GO

CREATE PROCEDURE sp_NewAlbum
@name AS VARCHAR(20)
AS

INSERT INTO Albums ([name])
VALUES (@name)

RETURN @@identity
GO

Thread1
November 4th, 2008, 09:23 PM
it seems that you are not saving the paramaters..

'cmd.Parameters.Add(param) :p


by the way, why are you not using your stored procedure?

aamir55
November 4th, 2008, 09:40 PM
i m saving parameter for each
by the way how to use stored procedure??

Thread1
November 4th, 2008, 09:43 PM
ah i see :D i overlooked that..

there is plenty on the web, just google :wave:

aamir55
November 5th, 2008, 05:37 AM
if i send u my source file then can u help me??