Results 1 to 2 of 2

Thread: SQL Parameter collection problem

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159

    SQL Parameter collection problem

    I have created a form that will upload an image to an sql server

    the data stored in the database will be:

    pictureTitle
    PictureDescription
    PictureOwner
    pictureData

    each time i try and insert the information into the database the following error occures


    An unhandled exception of type 'System.IndexOutOfRangeException' occurred in system.data.dll

    Additional information: An SqlParameter with ParameterName '@pictureTitle' is not contained by this SqlParameterCollection.


    Here is my code.. can u notice the problem please help..

    Code:
    Imports System.IO
    Imports System.Data.SqlClient
    
    
        Public FileName As String
        Public PictureTitle As String
        Public PictureDescription As String
        Public PictureOwner As String = System.Environment.UserName
    Code:
    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
    
            Dim opendialog As New OpenFileDialog
    
    
            opendialog.DefaultExt = "*.jpg"
            opendialog.Filter = "Picture files(*.jpg)|*.jpg|Picture files_ (*.jpeg)|*.jpeg|Picture files (*.gif)|*.gif "
    
            If (opendialog.ShowDialog() = DialogResult.OK) Then
                Me.PictureBox1.Image = New Bitmap(opendialog.FileName)
    
                FileName = opendialog.FileName
    
            End If
    
            PictureTitle = Path.GetFileName(opendialog.FileName)
            TextBox1.Text = PictureTitle
    
        End Sub
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            SqlConnection1.Open()
    
            Dim myCommand As New SqlCommand("Insert INTO Pictures(pictureTitle, pictureDescription, pictureOwner, pictureData) values(@pictureTitle, @pitureDescription, @pictureOwner, @pictureData)", SqlConnection1)
    
            'Read jpg into file stream, and from there into Byte array.
            Dim myFileStream As New FileStream(FileName, FileMode.Open, FileAccess.Read)
    
            Dim arrayOfBytes(myFileStream.Length) As [Byte]
    
            myFileStream.Read(arrayOfBytes, 0, arrayOfBytes.Length)
    
            myFileStream.Close()
    
            'Copy entered values into parameters
            myCommand.Parameters("@pictureTitle").Value = PictureTitle
            myCommand.Parameters("@pictureDescription").Value = PictureDescription
            myCommand.Parameters("@pictureOwner").Value = PictureOwner
    
            Dim myParameter As New SqlParameter("@pictureData", SqlDbType.VarBinary, arrayOfBytes.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, arrayOfBytes)
            myCommand.Parameters.Add(myParameter)
    
            'Create variable for returned value from SQL execution
            Dim SQLreturn As Integer
    
            'Protect against problems using a Try...Catch block
            Try
                'Execute the command
                SQLreturn = myCommand.ExecuteNonQuery()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
    
            End Try
    
            'Report back results
            If SQLreturn <> 1 Then
                MessageBox.Show("Could not execute the INSERT query")
            Else
                MessageBox.Show("INSERT completed successfully")
            End If
    
            SqlConnection1.Close()
    Last edited by NOTSOSURE; Feb 19th, 2004 at 08:44 AM.

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