vb.net, sql server 2005
HEllo All
i have a code which allow me to upload small files , but doies'nt allow me to upload DOC/PDF/Tiff Files to database
i am getting
Out of memory. error
kindly advise
thanks
HEllo All
i have a code which allow me to upload small files , but doies'nt allow me to upload DOC/PDF/Tiff Files to database
i am getting
Out of memory. error
kindly advise
VB.NET:
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Public Class Form1
Private mImageFile As Image
Private mImageFilePath As String
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
OpenFileDialog1.Title = "Set Image File"
OpenFileDialog1.Filter = "Bitmap Files|*.bmp|Gif Files|*.gif|JPEG Files|*.jpg|PDF Files|*.pdf|DOC Files|*.doc"
OpenFileDialog1.DefaultExt = "bmp"
OpenFileDialog1.FilterIndex = 1
OpenFileDialog1.FileName = ""
OpenFileDialog1.ShowDialog()
Dim sFilePath As String
sFilePath = OpenFileDialog1.FileName
If sFilePath = "" Then Exit Sub
If System.IO.File.Exists(sFilePath) = False Then
Exit Sub
Else
txtImageFile.Text = sFilePath
mImageFilePath = sFilePath
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If (Me.txtImageFile.Text = String.Empty Or Me.txtTitle.Text = String.Empty) Then
MessageBox.Show("Complete both form fields prior to submitting", "Missing Values", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "File Test Error")
End Try
Dim fs As FileStream = New FileStream(mImageFilePath.ToString(), FileMode.Open)
Dim img As Byte() = New Byte(fs.Length) {}
fs.Read(img, 0, fs.Length)
fs.Close()
mImageFile = Image.FromFile(mImageFilePath.ToString())
Dim imgHeight As Integer = mImageFile.Height
Dim imgWidth As Integer = mImageFile.Width
Dim imgLength As Integer = mImageFile.PropertyItems.Length
Dim imgType As String = Path.GetExtension(mImageFilePath)
mImageFile = Nothing
Dim strConnect As String
strConnect = "Data Source=sajjad-laptop;Initial Catalog=master;User ID=sa ; password=sajjad"
Dim conn As SqlConnection = New SqlConnection(strConnect)
Dim sSQL As String = "INSERT INTO store_image(ImageContent, ImageTitle, ImageType, ImageHeight, ImageWidth) VALUES( @pic, @title, @itype, @iheight, @iwidth )"
Dim cmd As SqlCommand = New SqlCommand(sSQL, conn)
' image content
Dim pic As SqlParameter = New SqlParameter("@pic", SqlDbType.Image)
pic.Value = img
cmd.Parameters.Add(pic)
' title
Dim title As SqlParameter = New SqlParameter("@title", System.Data.SqlDbType.VarChar, 50)
title.Value = txtTitle.Text.ToString()
cmd.Parameters.Add(title)
' type
Dim itype As SqlParameter = New SqlParameter("@itype", System.Data.SqlDbType.Char, 4)
itype.Value = imgType.ToString()
cmd.Parameters.Add(itype)
' height
Dim iheight As SqlParameter = New SqlParameter("@iheight", System.Data.SqlDbType.Int)
iheight.Value = imgHeight
cmd.Parameters.Add(iheight)
' width
Dim iwidth As SqlParameter = New SqlParameter("@iwidth", System.Data.SqlDbType.Int)
iwidth.Value = imgWidth
cmd.Parameters.Add(iwidth)
Try
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
MessageBox.Show("Query executed.", "Image Load")
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Data Error")
Exit Sub
End Try
End Sub
End Class
thanks
Last edited: