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

G

Guest

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").Value) > 0 Then
'Return Convert.ToInt32(oCm.Parameters("@ret").Value)
If oCm.Parameters("RETURN_VALUE").Value.ToString.Length > 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(CommandBehavior 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.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain() +1292
Page 1 of 1 Invalid cast from System.Int32 to System.Byte[].

Regards,
 
W

William F. Robertson, Jr.

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 said:
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").Value) > 0 Then
'Return Convert.ToInt32(oCm.Parameters("@ret").Value)
If oCm.Parameters("RETURN_VALUE").Value.ToString.Length > 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(CommandBehavior 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.IPostBackEventHandler.RaisePo
stBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain() +1292
Page 1 of 1 Invalid cast from System.Int32 to System.Byte[].

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top