Question

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)
Was it helpful?

Solution

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top