Print Page | Close Window

storing images in an image column

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=17536
Printed Date: 29 March 2026 at 6:51pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: storing images in an image column
Posted By: Leeb65
Subject: storing images in an image column
Date Posted: 14 December 2005 at 5:38am
Hi all,
 
I have a big problem that needs to be resolved.
 
I am using SQL Server and ASP.NET (C#) and I have to store the Images in the DB. I have the fields already and there are already images in the DB, but I did not write the code to insert the images and I don't have the source for it.
 
Does anyone know how this is done?
 
I know the field is a binary field, but it's getting the image in there is the problem.
 
Thanks in advance,
 


-------------
Lee

http://www.rheinsitemedia.de">



Replies:
Posted By: michael
Date Posted: 14 December 2005 at 10:37am
Basically you convert it to byte() and just store it.
 

Dim length As Integer = CInt(FileUpload.PostedFile.InputStream.Length)
                
Dim contentType As String = FileUpload.PostedFile.ContentType
              
Dim content(length) As Byte
            
FileUpload.PostedFile.InputStream.Read(content, 0, length)
              
documents.UpdateDocument(moduleId, itemId, Context.User.Identity.Name, NameField.Text, PathField.Text, CategoryField.Text, content, length, contentType)
 
and then my upload class looks like this:
 

Public Sub UpdateDocument(ByVal moduleId As Integer, ByVal itemId As Integer, ByVal userName As String, ByVal name As String, ByVal url As String, ByVal category As String, ByVal content() As Byte, ByVal size As Integer, ByVal contentType As String)
 
            If userName.Length < 1 Then

                userName = "unknown"

            End If
 
            ' Create Instance of Connection and Command Object

            Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

            Dim myCommand As New SqlCommand("Portal_UpdateDocument", myConnection)

            myCommand.CommandType = CommandType.StoredProcedure
 
             Dim parameterItemID As New SqlParameter("@ItemID", SqlDbType.Int, 4)

            parameterItemID.Value = itemId

            myCommand.Parameters.Add(parameterItemID)
 
            Dim parameterModuleID As New SqlParameter("@ModuleID", SqlDbType.Int, 4)

            parameterModuleID.Value = moduleId

            myCommand.Parameters.Add(parameterModuleID)
 
            Dim parameterUserName As New SqlParameter("@UserName", SqlDbType.NVarChar, 100)

            parameterUserName.Value = userName

            myCommand.Parameters.Add(parameterUserName)
 
            Dim parameterName As New SqlParameter("@FileFriendlyName", SqlDbType.NVarChar, 150)

            parameterName.Value = name

            myCommand.Parameters.Add(parameterName)
            Dim parameterFileUrl As New SqlParameter("@FileNameUrl", SqlDbType.NVarChar, 250)

            parameterFileUrl.Value = url

            myCommand.Parameters.Add(parameterFileUrl)
 
            Dim parameterCategory As New SqlParameter("@Category", SqlDbType.NVarChar, 50)

            parameterCategory.Value = category

            myCommand.Parameters.Add(parameterCategory)
 
            Dim parameterContent As New SqlParameter("@Content", SqlDbType.Image)

            parameterContent.Value = content

            myCommand.Parameters.Add(parameterContent)
 
            Dim parameterContentType As New SqlParameter("@ContentType", SqlDbType.NVarChar, 50)

            parameterContentType.Value = contentType

            myCommand.Parameters.Add(parameterContentType)
 
            Dim parameterContentSize As New SqlParameter("@ContentSize", SqlDbType.Int, 4)

            parameterContentSize.Value = size

            myCommand.Parameters.Add(parameterContentSize)
 
            myConnection.Open()

            myCommand.ExecuteNonQuery()

            myConnection.Close()
 
        End Sub

 
 
 
hope this helps. Now you may be able to strip some parameters if you don't need them.
 
 
 
 


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: michael
Date Posted: 14 December 2005 at 10:41am
sory to stretch it out like this, but this RTE does not like pastes from VS.
 
Gruss
Michael


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Leeb65
Date Posted: 14 December 2005 at 11:08am
Thanks Michael.
 
It works a treat.
 


-------------
Lee

http://www.rheinsitemedia.de">



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net