Failed to convert parameter value from a string to a int32

I am not sure where my app is failing but most probably this is where it is failing… I think lblExpID.Text is the culprit. Here’s the relvant part of my code:
Dim lblExpID As Label
For Each item In LSGrid.Rows
lblExpID = item.FindControl(“lblID”)

If txtExpGenFund.Text <> “” Then
sqlArray(i) = ExpDetailsID & “,” & (lblExpID.Text) & “,” & MunID & “,” & _
Val(txtExpGenFund.Text) & “,” & Val(txtExpDetSpecialRevenue.Text) & “,” & _
Val(txtExpDetCapitalProjects.Text) & “,” & expTotal & “,” & reportingYear & “,” & “Test Comments” & “,” & UserID
End If

Can u help me identify why i am getting this error looking at the sp below:
PROCEDURE [dbo].[uspEXPENDITURE_DETAILS]
(
@ExpDetailsID int=0 output,
@ExpDet_ExpID int,
@ExpDet_Municipality_ID nvarchar(50),
@ExpDetGeneralFund money,
@ExpDetSpecialRevenue money,
@ExpDetCapitalProjects money,
@ExpDetTotal money,
@Exp_Reporting_Year varchar(10),
@ExpComments varchar(max),
@Contact_ID int

)

AS
BEGIN
– SET NOCOUNT ON added to prevent extra result sets from
– interfering with SELECT statements.
SET NOCOUNT ON;

delete from [dbo].[TBL_EXPENDITURE_DETAILS] where ExpDet_ExpID=@ExpDet_ExpID and ExpDet_Municipality_ID=@ExpDet_Municipality_ID and Exp_Reporting_Year=@Exp_Reporting_Year

If @ExpDetailsID >= 0
Begin

INSERT INTO [dbo].[TBL_EXPENDITURE_DETAILS]
([ExpDet_ExpID]
,[ExpDet_Municipality_ID]
,[ExpDetGeneralFund]
,[ExpDetSpecialRevenue]
,[ExpDetCapitalProjects]
,[ExpDetTotal]
,[Exp_Reporting_Year]
,[ExpComments]
,[ExpDetCreatedBy]
,[ExpDetCreateDate])
VALUES
(@ExpDet_ExpID,
@ExpDet_Municipality_ID,
@ExpDetGeneralFund,
@ExpDetSpecialRevenue,
@ExpDetCapitalProjects,
@ExpDetTotal,
@Exp_Reporting_Year,
@ExpComments,
@Contact_ID,
GetDate()

)

SET @ExpDetailsID = SCOPE_IDENTITY()
return @ExpDetailsID

end

As far as I recall, with VB you can concat string and ints into a single string without converting. I could be wrong though so don’t take it for granted. Anyway, the result of &ing things together like that will be a string.

Part of the problem in us helping is that we aren’t seeing all the code or how it’s being used in conjunction with the stored proc. I am assuming this bit is in a loop to incremement the index “i”? Anyway, since you’re assigning a long concatenated string to that array index, try setting it to a value first, and either put a watch, or beakpoint on it to see if that’s where the error actually is.

Dim testVal as String

testVal = ExpDetailsID & “,” & (lblExpID.Text) & “,” & MunID & “,” & _ ’ plus the rest
sqlArray(i) = testVal ’ set break here and inspect testVal

If it makes it there without causing an exception, and testVal has the right value, then the error is somewhere else.

I fyour intent is to collect params for the stored proc, then you probably need to use a multidimensional array and set each element to a specific value. It looks like you’re hoping the commas you’re including will be enough to delimit params.

for r = 0 to rowCount step 1

sqlArray(r, ID1_INDEX) = id1
sqlArray(r, ID2_INDEX) = TextBox1.Text
’ and so one, loop through the rows, and use consts to define your param indexes.

end for

My VB syntax might be a bit off, it’s been about 5 years since I’ve touched it. And if my hunches here are wrong also, just disregard the post.

Glad it worked :slight_smile:

Is this VB Script (ASP)? Might want to move it to the classic ASP Forum. Then again, I don’t use VB.NET, just looks different.

ExpDet_ExpID is an integer in your stored procedure, you need to parse lblExpID.Text before you pass it into the query.

Int32.Parse(lblExpID.Text)

Does it give you a line number? In my experience a stored procedure error is typically not that specific; I usually get “Something went screwy” and you dig a little deeper and find that it probably is a problem down in the stored proc. So by the error message you’re getting, I would actually suspect that the problem is in a line of the program code. Possibly sneaky one where you’ve implicitly passed a string that looks like an int to another variable.

Odd, the two posts above mine weren’t showing when I posted. Glad you got it worked out.

YOU R THE BEST Jamie. Ya it worked. Three cheers to you…:slight_smile:

Ok, for starters, if lblExpID.Text fails in it’s conversion to Int what do you want to happen ?

My reply still stands for converting to Int, VB would be something like:

Dim iExpId As Integer
If (Int32.TryParse(lblExpID.Text, iExpId)) Then
// valid int
Else
// invalid int
End If

This is VB.NET. I am using VS framework 3.5

Nothing is working so far…i have tried everything…please help… I need to submit this day after tomorrow:-(

GOT MY REPLY… please follow JamieTownsend answers if you have similar issue

do not use parse as it will return an exception if fails.

use something like this:

int iExpId;
if (Int32.TryParse(lblExpID.Text, out iExpId))
{
// valid int
}

else
// fail or do something else