CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: saving image

  1. #1
    Join Date
    Mar 2008
    Posts
    142

    saving image

    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.
    Code:
    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
    Code:
    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

  2. #2
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: saving image

    it seems that you are not saving the paramaters..

    'cmd.Parameters.Add(param)


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

  3. #3
    Join Date
    Mar 2008
    Posts
    142

    Re: saving image

    i m saving parameter for each
    by the way how to use stored procedure??

  4. #4
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: saving image

    ah i see i overlooked that..

    there is plenty on the web, just google
    Busy

  5. #5
    Join Date
    Mar 2008
    Posts
    142

    Re: saving image

    if i send u my source file then can u help me??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured