By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,154 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,154 IT Pros & Developers. It's quick & easy.

Invalid cast from System.Int32 to System.Byte[].

P: n/a
Hi,
I got a stored procedure, where it returns a value. But if I execute it. It
gives an error as "Invalid cast from System.Int32 to System.Byte[].". To make
clear how do I execute this, below I'm specifiying my code:

The Code used in Visual Studio:

Function GetRank(ByVal ID As Integer, ByVal Comp As String, ByVal Sec As
String, ByVal iDate As Date) As String
'Dim Ret As Integer
Dim oCm As New SqlCommand("spGetShareholderRank", connection)
'@ID as VarChar, @Comp as VarChar, @iDate as DateTime, @sec as Varchar

oCm.CommandType = CommandType.StoredProcedure
oCm.Parameters.Add(New SqlParameter("RETURN_VALUE",
ParameterDirection.Output, SqlDbType.Int))
oCm.Parameters.Add(New SqlParameter("@ID", ParameterDirection.Input,
SqlDbType.Int)).Value = ID
oCm.Parameters.Add(New SqlParameter("@Comp",
ParameterDirection.Input, SqlDbType.VarChar)).Value = Comp
oCm.Parameters.Add(New SqlParameter("@iDate",
ParameterDirection.Input, SqlDbType.DateTime)).Value = iDate
oCm.Parameters.Add(New SqlParameter("@sec",
ParameterDirection.Input, SqlDbType.VarChar)).Value = Sec

Try
oCm.ExecuteNonQuery()
'Ret = Convert.ToInt32(oCm.Parameters("@ret").Value)
'If Convert.ToInt32(oCm.Parameters("RETURN_VALUE").Val ue) > 0 Then
'Return Convert.ToInt32(oCm.Parameters("@ret").Value)
If oCm.Parameters("RETURN_VALUE").Value.ToString.Leng th > 0 Then
Return oCm.Parameters("RETURN_VALUE").Value.ToString
Else
Return 0
End If
Finally
oCm.Dispose()
End Try
End Function

And this is how I created my Stored Procedure:

CREATE PROCEDURE [spGetShareholderRank]
@ID Int,
@Comp VarChar(10),
@iDate DateTime,
@sec Varchar(10),
@ret Int OUTPUT
AS
DECLARE @Rank Int

SELECT @ret = ShareBalance.Rank
FROM ShareBalance INNER JOIN Lists ON ShareBalance.ListID = Lists.ListID
WHERE ShareBalance.ShareHolderNo=@ID
AND Lists.CompID=@Comp
AND Lists.IssueDate= @iDate
AND Lists.SecurityCode=@sec

IF (@ret) <= 0
SET @ret = 0
GO

Is there anything wrong with my Codes? I did test'd my stored procedure in
Query Analyzer where it worked fine.

Note:
The Error in Detail:
Server Error in '/grid' Application.
Invalid cast from System.Int32 to System.Byte[].
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.InvalidCastException: Invalid cast from
System.Int32 to System.Byte[].
Source Error:
Source File: c:\inetpub\wwwroot\grid\Differentiate.aspx.vb Line: 314
Stack Trace:
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032
Line 312:
Line 313: Try
Line 314: oCm.ExecuteNonQuery()
Line 315: 'Ret = Convert.ToInt32(oCm.Parameters("@ret").Value)
Line 316: If Convert.ToInt32(oCm.Parameters("@ret").Value) > 0 Then
[InvalidCastException: Invalid cast from System.Int32 to System.Byte[].]
System.Data.SqlClient.SqlCommand.ExecuteReader(Com mandBehavior cmdBehavior,
RunBehavior runBehavior, Boolean returnStream) +742
System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
grid.Differentiate.GetRank(Int32 ID, String Comp, String Sec, DateTime
iDate) in c:\inetpub\wwwroot\grid\Differentiate.aspx.vb:314
grid.Differentiate.Submitbtn_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\grid\Differentiate.aspx.vb:158
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl,
String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
System.Web.UI.Page.ProcessRequestMain() +1292
Page 1 of 1 Invalid cast from System.Int32 to System.Byte[].

Regards,

--
Mohammed Hifni Shahzard Nazeer,
JB Securities Pvt. Ltd.,
Colombo.
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In your stored procedure you have a parameter called @Ret int OUTPUT. You
are never creating the @ret output parameter.

oCm.Parameters.Add(New SqlParameter("@ret", ParameterDirection.Output,
SqlDbType.Int))

When you get the value back out, you can directly cast this.

Dim ret as int;
ret = CType( oCm.Parameters("@ret").Value, int )

HTH,

bill

"Hifni Shahzard" <Hi***********@discussions.microsoft.com> wrote in message
news:90**********************************@microsof t.com...
Hi,
I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte[].". To make clear how do I execute this, below I'm specifiying my code:

The Code used in Visual Studio:

Function GetRank(ByVal ID As Integer, ByVal Comp As String, ByVal Sec As
String, ByVal iDate As Date) As String
'Dim Ret As Integer
Dim oCm As New SqlCommand("spGetShareholderRank", connection)
'@ID as VarChar, @Comp as VarChar, @iDate as DateTime, @sec as Varchar
oCm.CommandType = CommandType.StoredProcedure
oCm.Parameters.Add(New SqlParameter("RETURN_VALUE",
ParameterDirection.Output, SqlDbType.Int))
oCm.Parameters.Add(New SqlParameter("@ID", ParameterDirection.Input, SqlDbType.Int)).Value = ID
oCm.Parameters.Add(New SqlParameter("@Comp",
ParameterDirection.Input, SqlDbType.VarChar)).Value = Comp
oCm.Parameters.Add(New SqlParameter("@iDate",
ParameterDirection.Input, SqlDbType.DateTime)).Value = iDate
oCm.Parameters.Add(New SqlParameter("@sec",
ParameterDirection.Input, SqlDbType.VarChar)).Value = Sec

Try
oCm.ExecuteNonQuery()
'Ret = Convert.ToInt32(oCm.Parameters("@ret").Value)
'If Convert.ToInt32(oCm.Parameters("RETURN_VALUE").Val ue) > 0 Then 'Return Convert.ToInt32(oCm.Parameters("@ret").Value)
If oCm.Parameters("RETURN_VALUE").Value.ToString.Leng th > 0 Then Return oCm.Parameters("RETURN_VALUE").Value.ToString
Else
Return 0
End If
Finally
oCm.Dispose()
End Try
End Function

And this is how I created my Stored Procedure:

CREATE PROCEDURE [spGetShareholderRank]
@ID Int,
@Comp VarChar(10),
@iDate DateTime,
@sec Varchar(10),
@ret Int OUTPUT
AS
DECLARE @Rank Int

SELECT @ret = ShareBalance.Rank
FROM ShareBalance INNER JOIN Lists ON ShareBalance.ListID = Lists.ListID
WHERE ShareBalance.ShareHolderNo=@ID
AND Lists.CompID=@Comp
AND Lists.IssueDate= @iDate
AND Lists.SecurityCode=@sec

IF (@ret) <= 0
SET @ret = 0
GO

Is there anything wrong with my Codes? I did test'd my stored procedure in
Query Analyzer where it worked fine.

Note:
The Error in Detail:
Server Error in '/grid' Application.
Invalid cast from System.Int32 to System.Byte[].
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Invalid cast from
System.Int32 to System.Byte[].
Source Error:
Source File: c:\inetpub\wwwroot\grid\Differentiate.aspx.vb Line: 314
Stack Trace:
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032
Line 312:
Line 313: Try
Line 314: oCm.ExecuteNonQuery()
Line 315: 'Ret = Convert.ToInt32(oCm.Parameters("@ret").Value)
Line 316: If Convert.ToInt32(oCm.Parameters("@ret").Value) > 0 Then
[InvalidCastException: Invalid cast from System.Int32 to System.Byte[].]
System.Data.SqlClient.SqlCommand.ExecuteReader(Com mandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +742
System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
grid.Differentiate.GetRank(Int32 ID, String Comp, String Sec, DateTime
iDate) in c:\inetpub\wwwroot\grid\Differentiate.aspx.vb:314
grid.Differentiate.Submitbtn_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\grid\Differentiate.aspx.vb:158
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePo
stBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl,
String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
System.Web.UI.Page.ProcessRequestMain() +1292
Page 1 of 1 Invalid cast from System.Int32 to System.Byte[].

Regards,

--
Mohammed Hifni Shahzard Nazeer,
JB Securities Pvt. Ltd.,
Colombo.

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.