Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Parameter Invalid: Uploading Picture in MS SQL

$
0
0
So I have a class for connecting to db and for the execution of queries there. This class is called "DBHelper". I won't spoil this class much 'cause I don't have a problem there. The problem with my code is that I can upload the picture successfully to the database, but when it comes to VB .Net, There is an error saying "Parameter Invalid".

This code is for my store procedure:
USE [testing]
GO
/****** Object:  StoredProcedure [dbo].[insertImage]    Script Date: 01/01/2013 10:17:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[insertImage]

@pic image
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

  insert into ImageTable(SImage) values (@pic)
END





This is my code for uploading picture:
Imports System.Windows.Forms
Imports System.IO
Public Class Form1
    Public cs, command As String
    Private mImageFile As Image
    Private mImageFilePath As String
    Public DBConhelper As DBHelper = Nothing
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            cs = "Data Source = NINJANINA\SQLEXPRESS; Initial Catalog = testing; Persist Security Info = False; User ID = sa; Password= sa"
            Me.DBConhelper = New DBHelper(cs)
            Me.DBConhelper.ConnectToDatabase()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        ' My Resource:  http://devpod.wordpress.com/2011/12/04/viewing-saving-an-image-to-ms-sql-server-in-vb-net/
        Dim fdlg As OpenFileDialog = New OpenFileDialog
        fdlg.Title = "Set Photo"
        fdlg.InitialDirectory = "C:\Users\Niña\Pictures"
        fdlg.Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"
        fdlg.FilterIndex = 4
        fdlg.FileName = ""
        fdlg.RestoreDirectory = True
        If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            tbImagePath.Text = fdlg.FileName
        Else
            Exit Sub
        End If
        Dim sFilePath As String
        sFilePath = fdlg.FileName
        If sFilePath = "" Then
            Exit Sub
        End If
        If System.IO.File.Exists(sFilePath) = False Then
            Exit Sub
        Else
            tbImagePath.Text = sFilePath
            mImageFilePath = sFilePath
        End If
        pbImage.Image = Image.FromFile(tbImagePath.Text)

    End Sub
    Private Function uploadFile()
       
        pbImage.Image.Dispose()
        Dim ok As Boolean = False
        Try
            If Me.tbImagePath.Text = String.Empty Then
                ok = False
                Return ok
                Exit Function
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Dim fs As FileStream = New FileStream(mImageFilePath.ToString(), IO.FileMode.Open)
        Dim img As Byte() = New Byte(fs.Length) {}
        fs.Read(img, 0, fs.Length)
        fs.Close()

        mImageFile = Image.FromFile(mImageFilePath.ToString())
        Dim imgType As String = Path.GetExtension(mImageFilePath)
        mImageFile = Nothing
        command = "insertImage"
        Dim pic As New System.Data.SqlClient.SqlParameter("@pic", SqlDbType.Image)
        pic.Value = img
        Try
            Me.DBConhelper.ExecuteSPNonQuery(command, pic)
            ok = True
        Catch ex As Exception
            MsgBox(ex.Message)
            ok = False
            Return ok
            Exit Function
        End Try

        Return ok
    End Function

    Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        uploadFile()
      
    End Sub

End Class




As you can see in my code, there includes my source. Thank you for any further help.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>