tags:

views:

553

answers:

3

I need to modify someone else's VB code, and I don't have much experience with VB6. I need to call a SQL2000 stored procedure using ADODB. One of the parameters is of type Binary and it's giving me problems. Every time I create the parameter, I get an error "Application uses a value of the wrong type for the current operation". The error happens at the cmd.parameter.append line, it doesn't even give me a chance call the cmd.execute.

Dim HexPassword As String
Dim BinPassword As String
Dim AsciiCode As Integer
Dim unitDigit As String
Dim TensDigit As String

Set obj_hash = New EDCrypt

' Returns Hash of password hex encoded
HexPassword = obj_hash.GetTextHash(Trim(txtPassword.text), haSHA1)

' Converts Hex Encoded string to Binary encoded string
Dim i As Integer


For i = 1 To 40 Step 2
    unitDigit = Mid(HexPassword, i + 1, 1)
    TensDigit = Mid(HexPassword, i, 1)
    AsciiCode = HexStrtoInt(TensDigit + unitDigit)
    BinPassword = BinPassword + Chr(AsciiCode)
Next i

conn.Open ConnectionString
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc

cmd.CommandText = "ValidatePasswordNew"
cmd.Parameters.Append cmd.CreateParameter("LoginID", adVarChar, adParamInput, 30, UserID)
cmd.Parameters.Append cmd.CreateParameter("ShaPassword", adBinary, adParamInput, 20, BinPassword)
+1  A: 

I think the problem may be in setting size = 20. That may be an optional argument. Try leaving it out. There might be a different overload of CreateParameter that you need to use here.

Check out this MSDN page which gives this general pattern:

command.CreateParameter (Name, Type, Direction, Size, Value)

Also, you have declared BinPassword as a String. You can't pass a string into a parameter of adBinary. You need to pass a binary object into that, or change adBinary to adVarChar.

DOK
I tried leaving the size out. I got "Parameter object is improperly defined. Inconsistent or incomplete information was provided"
Aheho
If I can't pass a string into a adBinary, what type do I use? I tried Variant and that didn't work either.
Aheho
Use adVarChar. See how that's used in the other parameter (UserID)? When you use adVarChar, it's good to use the size. So, if passwords don't exceed 20 characters, keep that.
DOK
If I change the type to adVarChar I get "Implicit conversion from data type varchar to binary is not allowed. Use the CONVERT function to run this query"This error occurs on cmd.execute
Aheho
+1  A: 

Try this for your string concatenation:

BinPassword = BinPassword & ChrB(AsciiCode)

+ is not the right string concatenation operator, and using ChrB should convince VB and ADO that you're really passing it binary data, not character data.

There's also a chance DOK is right about the size. You can try setting the size to something you're certain is longer than you need, or you can probably get around setting the size at all by splitting the operation into two statements:

cmd.Parameters.Append cmd.CreateParameter("ShaPassword", adBinary, adParamInput)
cmd.Parameters("ShaPassword").Value = BinPassword

It's weird, but it's worked for me in the past.

Just for added emphasis in case someone sees this again: the size property on Parameter objects does not have to match the exact byte length of your argument; it merely has to be at least big enough to hold your argument.

Dan
+1  A: 

You probably want adVarBinary. Most likely you should be passing Byte arrays as values.

Bob Riemersma