Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a class that accepts a command parameter as a parameter.

 

  Private cmd As SqlCommand
  Public Sub AppendParameter(ByVal p As SqlParameter)

     cmd.Parameters.Add(p)

  End Sub

 

The following code works perfectly:

cls.AppendParameter(New SqlParameter("@id", SqlDbType.VarChar, 25).Value = str)

 

But this code gives me a "Specified cast is not valid." error

cls.AppendParameter(New SqlParameter("@Manufacturer_ID", SqlDbType.Int).Value = 1)

 

If I create a parameter object and pass that, it works just fine. Any ideas?

 

Thanks,

 

Rob

  • Leaders
Posted (edited)

You actually got that to compile? I can't get anything similar to that to compile. Unless that is some new terminology.

 

Have you tried it like this?

Dim arg As SqlParameter = New SqlParameter("@Manufacturer_ID", SqlDbType.Int)

arg.Value = 1

cls.AppendParameter(arg)

:)

 

Or perhaps it's a SqlParameter thing. Which line gives you the error?

Edited by Iceplug

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • *Experts*
Posted

I would think it would work if all the "Option Strict" stuff were turned off...

 

When you pass in:

New SqlParameter("@id", SqlDbType.VarChar, 25).Value = str

 

I'm guessing that works the way it used to, which is:

Creates SqlParameter object

Compares the Value to str

Passes the result of the comparison (or assignment, can't remember) to your function.

 

If str is null, you might have "no problems", though you'd get some later.

 

If that is indeed the way it's working (which would explain why "Value = 1" throws a runtime error, because it would be passing "1" to the method), then you'll need to do what IcePlug suggested.

 

Or, as an alternative, why not enhance your method to something like:

AppendParameter(ByVal parameterName As String, ByBal type As SqlDbType, ByVal size As Integer32, Value As Object)

 

Basically, expose the params of the SqlParameter constructor as individual arguments PLUS the value.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Option Strict is turned off, but the working code does pass an actual value and works as expected. If it turn Option Strict on, it does give me an error. Seems to be unpredictable results.

 

I will move to a method based on Nerseus's suggestion.

 

Thanks

 

Rob

  • *Experts*
Posted

I tried your exact code to test my theory - I get a runtime error every time on either line you showed:

 

cls.AppendParameter(New SqlParameter("@Manufacturer_ID", SqlDbType.Int).Value = 1)

cls.AppendParameter(New SqlParameter("@id", SqlDbType.VarChar, 25).Value = str)

 

I set cls = New Class1 (my test class). I defined str as a string and set it to "hello".

 

Why not turn on Option Strict? If you do, it will catch little things like this. In general, I can't see why you'd ever want it off unless you were converting a large project and couldn't live with the compile errors...

 

 

Try this test. In your class that has the AppendParameter function, define the following:

   Public Sub Test(ByVal s As String)
       Debug.WriteLine(s)
   End Sub

 

Now in the calling class, try the following line instead of the AppendParameter call:

Dim str As String = "hello"
cls.Test(str = "howdy")

 

What you see in the Output window may surprise you. It isn't "hello" or "howdy", it's "False". The compare is evaluated and the result is passed to the method/Sub. This isn't C++ where the results is also an expression which is passed to the Sub. At least, I've never seen any version of VB or VBET do that.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...