I don't understand why I'm getting a casting error

L

Larry

I get a "Specified cast is not valid" when excuting the following line in
the subroutine below:

spParm = spAddImage.Parameters("@fnameIn").Value =
ckfile.Text

I'm not sure what I'm doing wrong to cause this error. I'm using the
following to set up this parameter:
spParm = spAddImage.Parameters.Add("@fnameIn",
SqlDbType.VarChar, 256)
spParm.Direction = ParameterDirection.Input

any ideas?

-Larry



Private Sub btnTransfer_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)HandlesbtnTransfer.Click
Dim srcpath, destpath As String
Dim ckfile As ListViewItem
' set up to execute the stored procedure
Dim spAddImage As New SqlClient.SqlCommand("spAddImage",
Me.SqlConnection1)
spAddImage.CommandType = CommandType.StoredProcedure
Dim spParm As SqlClient.SqlParameter
spParm = spAddImage.Parameters.Add("@fnameIn", SqlDbType.VarChar, 256)
spParm.Direction = ParameterDirection.Input
spParm = spAddImage.Parameters.Add("@fstatus", 2)
spParm = spAddImage.Parameters.Add("@srcDir", curntSrcDir)
spParm = spAddImage.Parameters.Add("@fnameOut", SqlDbType.VarChar, 256)
spParm.Direction = ParameterDirection.Output
spParm = spAddImage.Parameters.Add("@spError", SqlDbType.VarChar, 256)
spParm.Direction = ParameterDirection.Output
For Each ckfile In Me.lvSrcFileList.Items
If ckfile.Checked Then
srcpath = curntSrcDir & "\" & ckfile.Text
Try
If System.IO.File.Exists(srcpath) Then
' enter file into database and get the new file designation in return
spParm = spAddImage.Parameters("@fnameIn").Value =
ckfile.Text
Me.SqlConnection1.Open()
spAddImage.ExecuteNonQuery()
Me.SqlConnection1.Close()
If IsDBNull(spAddImage.Parameters("@spError").Value)
Then
errormsg(spAddImage.Parameters("@spError").Value)
Else
' now copy and rename the file
destpath = Me.txtDestination.Text & "\" &
spAddImage.Parameters("@fnameOut").Value
System.IO.File.Copy(srcpath, destpath)
ckfile.Checked = False
End If
End If
Catch ex As Exception
errormsg(ex.Message)
End Try
End If
Next
End Sub
 
E

EricJ

Try declaring your parameters as an array like this

Dim arParms() As SqlParameter = New SqlParameter(5) {}

and then fill the array

arParms(0) = New SqlParameter("@etiID", SqlDbType.Int)
arParms(0).Value = etiID
arParms(1) = New SqlParameter("@etiNaam", SqlDbType.VarChar, 50)
arParms(1).Value = etiNaam



hope it helps



eric
 
P

Peter Huang

Hi Larry,

You may need to break the line below into two
spParm = spAddImage.Parameters("@fnameIn").Value = ckfile.Text

spParm = spAddImage.Parameters("@fnameIn")
spParm.Value = ckfile

Since the ckfile.Text and spParm are different type, can not cast string
into SqlParameter.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
L

Larry

Thank you Peter for your promt response. Spliting up the line did the trick.
I don't quite understand why the ealier decalration wouldn't apply to the
value property for that particlular instance of spParm. At least it works
and I can move forward. As has been the case before you guys are great.
thanks again for the assistance.

-Larry
 
P

Peter Huang

Hi Larry,
spParm = spAddImage.Parameters("@fnameIn").Value = ckfile.Text
the first = is assignment operator
the second = is logical operator

e.g.
C="hello"

A=B=C
is equal to A=(B=C)

If B=C , then A will be true, otherwise A will be false, but B will not be
assign value.

Result:
A= False
B=Nothing
C="hello"

So,
spParm = spAddImage.Parameters("@fnameIn").Value = ckfile.Text
is equal to
spParm = (spAddImage.Parameters("@fnameIn").Value = ckfile.Text)
it will cast the logical operator result of
(spAddImage.Parameters("@fnameIn").Value = ckfile.Text)
to spParm( SqlParameter type)

Then you will get the "Specified cast is not valid" Error.


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top