hi

I am using VB6 and SQL Server 2000

I call a stored procedure to store a new course with its related subcourses.

The subcourses are sent in delemited text format.

In the SQL Server, the stored procedure calls a function that does splitting of the text and saves it in the table.

Using the analyzer it worked just fine.
But when calling it through VB it gives a conversion error
"Error converting data type varchar to int"

I here provide the stored procedure, the function and the vb code:

==> Table SubCourses contains type as integer

The VB Code
---------------------

VB Code:
  1. Function SaveRec() As Boolean
  2.  
  3. On Error GoTo ErrH
  4.  
  5. If Trim(txtCode.Text) = "" Then
  6. MsgBox "Enter Short Course Code", vbExclamation, Me.Caption
  7. SaveRec = False
  8. txtCode.SetFocus
  9. ElseIf Trim(txtNameE.Text) = "" Then
  10. MsgBox "Enter English Name", vbExclamation, Me.Caption
  11. SaveRec = False
  12. txtNameE.SetFocus
  13. Else
  14. Dim strSubCourses As String
  15. '----- Generate strSubCourses -------------------------------------------
  16. strSubCourses = ""
  17. For i = 1 To Grid.Rows - 1
  18. If Grid.TextMatrix(i, 1) <> "" Then
  19. strSubCourses = strSubCourses & Grid.TextMatrix(i, 1) & ";"
  20. End If
  21. Next i
  22. '------------------------------------------------------------------------
  23.  
  24. Dim Arg(0 To 9) As String 'Arg() array holds argument used by the save stored procedure
  25. Dim SPReturn As Long 'Return Value from the stored Procedure
  26. Arg(0) = "%" & CurrentID 'ID It searches for ID=0 and will not find it so a new record is added with a new ID extrcacted from Table "Table_SN"
  27. Arg(1) = "$5," & txtCode.Text
  28. Arg(2) = "$80," & txtNameE.Text
  29. Arg(3) = "$80," & txtNameA.Text
  30. Arg(4) = "$10," & txtRegCateg.Text
  31. Arg(5) = "@" & Val(txtFees.Text)
  32. Arg(6) = "$80," & strSubCourses
  33. Arg(7) = "$10," & CurrentUser 'User
  34. Arg(8) = "$255," & CurrentComputer 'Computer
  35. Arg(9) = "!0" ' 0 = Do not allow duplicate
  36. SPReturn = ExecuteSP("SaveSubCourse", Arg, CN)
  37. If SPReturn > 0 Then 'positive is the ID of the Record
  38. SaveRec = True
  39. CurrentID = SPReturn
  40. ElseIf SPReturn = -2 Then
  41. ans = MsgBox("The English Name is Duplicated, Do you wish to continue ?", vbYesNo + vbInformation, "Warning")
  42. If ans = vbYes Then 'yes
  43. Arg(10) = "!1" ' 1 = Allow duplicate
  44. SPReturn = ExecuteSP("SaveSubCourse", Arg, CN)
  45. Else '0=cancel
  46. SaveRec = False
  47. End If 'end if
  48. ElseIf SPReturn = -1 Then
  49. MsgBox "Code is Duplicated", vbExclamation, "Warning"
  50. SaveRec = False
  51. End If ' SPReturn
  52. End If ' New Rec
  53. Erase Arg 'Deallocates the array
  54. Exit Function
  55.  
  56. On Error GoTo 0
  57. Exit Function
  58.  
  59.  
  60. 'used to execute the Stored Procedure (SP)
  61.  
  62. Function ExecuteSP(SPName As String, Arguments() As String, ActiveCN As ADODB.Connection, Optional OutputRS As ADODB.Recordset) As Long
  63. Dim CMD As New ADODB.Command
  64. Dim Prm As New ADODB.Parameter
  65. Dim PrmType As String
  66. Dim PrmValue As String
  67. Dim temp() As String
  68. On Error GoTo ErrH
  69. Set CMD.ActiveConnection = ActiveCN
  70. CMD.CommandType = adCmdStoredProc
  71. CMD.CommandText = SPName
  72. Set Prm = CMD.CreateParameter("Return", adInteger, adParamReturnValue)
  73. CMD.Parameters.Append Prm
  74. For i = 0 To UBound(Arguments)
  75. Set Prm = New ADODB.Parameter
  76. PrmType = Mid(Arguments(i), 1, 1)
  77. PrmValue = Mid(Arguments(i), 2)
  78. Select Case PrmType
  79. Case "#" ' Date
  80. temp = Split(PrmValue, ",")
  81. Prm.Type = adDate
  82. Prm.Value = DateSerial(Val(temp(2)), Val(temp(1)), Val(temp(0)))
  83. Case "$" ' String
  84. temp = Split(PrmValue, ",")
  85. Prm.Type = adVarChar
  86. Prm.Size = Val(temp(0))
  87. Prm.Value = temp(1)
  88. Case "%" ' Long
  89. Prm.Type = adInteger
  90. Prm.Value = Val(PrmValue)
  91. Case "@" ' Single (Float)
  92. Prm.Type = adSingle
  93. Prm.Value = Val(PrmValue)
  94. Case "!" ' Boolean
  95. Prm.Type = adBoolean
  96. Prm.Value = Val(PrmValue)
  97. Case "*" 'Text
  98. Prm.Type = adVarChar
  99. Prm.Value = Val(PrmValue)
  100. ' Prm.Value = IIf(PrmValue = "", Null, PrmValue)
  101. End Select
  102. CMD.Parameters.Append Prm
  103. Next i
  104. If OutputRS Is Nothing Then
  105. CMD.Execute
  106. Else
  107. Set OutputRS = CMD.Execute
  108. End If
  109. ExecuteSP = CMD.Parameters(0).Value
  110. For i = 0 To CMD.Parameters.Count - 1
  111. CMD.Parameters.Delete (0)
  112. Next i
  113. Set Prm = Nothing
  114. Set CMD = Nothing
  115. Exit Function
  116.  
  117. On Error GoTo 0
  118. Exit Function
  119.  
  120. ErrH:
  121. MsgBox "Error No.:" & Err.Number & vbNewLine & "Module:Functions" & vbNewLine & "Function:ExecuteSP" & vbNewLine & "Description:" & Err.Description, vbExclamation, "WARNING !"
  122. Set Prm = Nothing
  123. Set CMD = Nothing
  124. End Function

The Stored Procedure
------------------------------------

/*
This SP is used to update and insert Courses.
If the procedure succeeds it will return +ve value representing the new or current Course Code
else it will return -ve value representing an error :
(-1) The code is duplicated
(-2) The Name is duplicated (If @AllowDuplecateName is 0 only)
(-3) Connot get new id
(-100) Unknown Error
*/
CREATE PROCEDURE SaveCourse
@ID INT,
@Code VARCHAR(5), -- Course Code
@NameE VARCHAR(80), -- Course Name (Eglish Name)
@NameA VARCHAR(80),
@RegCat VARCHAR(10),
@Fees FLOAT, -- Course Fees
@SubCourses VARCHAR(80), -- Delimited string contains SubCourses ID's like '10;23;45;65; ..... ;78;'
@CurrentUser VARCHAR(10), -- Current user in application
@CurrentComputer VARCHAR(255),
@AllowDuplicateName BIT -- If @AllowDuplicateName=1 then user can enter many Courses with same name
AS
SET NOCOUNT ON
DECLARE @Temp INT
DECLARE @NewID INT
DECLARE @strTemp VARCHAR(100)

--1) Determine if the Course name (Eglish Name) is duplicated
SELECT @Temp = [ID] FROM Courses WHERE namee=@NameE AND [ID] <>@ID
IF @@ROWCOUNT>0 AND @AllowDuplicateName=0 RETURN(-2) --The name is duplicated

-- 2) Determine if the Course will be updated or inserted
SELECT @Temp = [ID] FROM Courses WHERE [ID]=@ID
IF @@ROWCOUNT=0 -- New Record
BEGIN
-- Make sure that the code is not duplicated
SELECT @Temp = [code] FROM Courses WHERE Code=@Code
IF @@ROWCOUNT>0 RETURN(-1) --The code is duplicated
EXEC GetNewID 'Courses', @NewID OUTPUT
IF @NewID<0 RETURN(-3)
BEGIN TRANSACTION
INSERT INTO Courses ([ID], code, NameE,NameA, Reg_Category, Fees, EntryUser, EntryDate) VALUES (@NewID, @code, @NameE, @NameA, @RegCat, @Fees, @CurrentUser, GETDATE())
IF @@ERROR<>0
BEGIN
ROLLBACK
RETURN (-100)
END
INSERT INTO Courses_SubCourses SELECT @NewID, CAST(Col1 AS INT) FROM Split(@SubCourses, ',', ';')
IF @@ERROR<>0
BEGIN
ROLLBACK
RETURN (-100)
END
COMMIT TRANSACTION
SET @strTemp='Insert COURCE ID = ' + CAST(@Temp as varchar(10)) + ' Code = ' + @Code
EXEC AddToLog @CurrentUser, @CurrentComputer, 'INSERT', 'COURCES', @strTemp
IF @@ERROR<>0
RETURN (-100)
ELSE
RETURN @NewID
END

ELSE -- Update Record
BEGIN
BEGIN TRANSACTION
UPDATE Courses SET code=@code, NameE=@NameE,NameA=@NameA, Reg_Category=@RegCat, Fees=@Fees, EditUser=@CurrentUser, EditDate=GETDATE() WHERE [ID] = @ID
IF @@ERROR<>0
BEGIN
ROLLBACK
RETURN (-100)
END
DELETE Courses_SubCourses WHERE Course_ID = @ID
IF @@ERROR<>0
BEGIN
ROLLBACK
RETURN (-100)
END

INSERT INTO Courses_SubCourses SELECT @ID, Col1 FROM Split(@SubCourses, ',', ';')
IF @@ERROR<>0
BEGIN
ROLLBACK
RETURN (-100)
END

COMMIT TRANSACTION
SET @strTemp='Update COURCE ID = ' + CAST(@Temp as varchar(10)) + ' Code = ' + @Code
EXEC AddToLog @CurrentUser, @CurrentComputer, 'UPDATE', 'COURCES', @strTemp
IF @@ERROR<>0
RETURN (-100)
ELSE
RETURN @ID
END
GO

The User Defined Function
-----------------------------------------
CREATE FUNCTION dbo.Split (@Data VARCHAR(80), @ColDelimiter CHAR(1)=',', @RowDelimiter CHAR(1)=';')
RETURNS @Result TABLE (
Col1 VARCHAR(100),
Col2 VARCHAR(100),
Col3 VARCHAR(100),
Col4 VARCHAR(100),
Col5 VARCHAR(100),
Col6 VARCHAR(100),
Col7 VARCHAR(100),
Col8 VARCHAR(100),
Col9 VARCHAR(100),
Col10 VARCHAR(100)
)
BEGIN
DECLARE @ColVal VARCHAR(100)
DECLARE @CurrCol TINYINT
DECLARE @Length INT
DECLARE @i INT
DECLARE @Ch CHAR(1)
DECLARE @Col1 VARCHAR(100),
@Col2 VARCHAR(100),
@Col3 VARCHAR(100),
@Col4 VARCHAR(100),
@Col5 VARCHAR(100),
@Col6 VARCHAR(100),
@Col7 VARCHAR(100),
@Col8 VARCHAR(100),
@Col9 VARCHAR(100),
@Col10 VARCHAR(100)
SET @Col1='0'
SET @Col2='0'
SET @Col3='0'
SET @Col4='0'
SET @Col5='0'
SET @Col6='0'
SET @Col7='0'
SET @Col8='0'
SET @Col9='0'
SET @Col10='0'
SET @ColVal=''
SET @CurrCol=1
SET @i=1
SET @Length=DATALENGTH(@Data)
WHILE @i<=@Length
BEGIN
SET @Ch=SUBSTRING(@Data, @i, 1)
IF @Ch=@ColDelimiter OR @Ch = @RowDelimiter
BEGIN
IF @CurrCol=1
SET @Col1=@ColVal
ELSE IF @CurrCol=2
SET @Col2=@ColVal
ELSE IF @CurrCol=3
SET @Col3=@ColVal
ELSE IF @CurrCol=4
SET @Col4=@ColVal
ELSE IF @CurrCol=5
SET @Col5=@ColVal
ELSE IF @CurrCol=6
SET @Col6=@ColVal
ELSE IF @CurrCol=7
SET @Col7=@ColVal
ELSE IF @CurrCol=8
SET @Col8=@ColVal
ELSE IF @CurrCol=9
SET @Col9=@ColVal
ELSE IF @CurrCol=10
SET @Col10=@ColVal
SET @ColVal=''
SET @CurrCol=@CurrCol+1
IF @Ch = @RowDelimiter
BEGIN
INSERT INTO @Result VALUES(@Col1, @Col2, @Col3, @Col4,@Col5, @Col6,@Col7, @Col8,@Col9, @Col10)
SET @CurrCol=1
END
END
ELSE
BEGIN
SET @ColVal = @ColVal + @Ch
END
SET @i=@i+1
END
RETURN
END


Thank you in advance