473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
1 4816
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
0
by: Hifni Shahzard | last post by:
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...
0
by: Robert Smith | last post by:
I am getting the following error being caught in my application_error method... I have no code referencing the crypto classes. I have found nothing on the net outside of people with crypto...
2
by: Brent K | last post by:
Ok, I have an internal intranet website created in visual studio 2005, c#. It was running fine for months, and then all of the sudden a few days I started getting these errors emailed to me (I have...
1
by: rdlauer | last post by:
For some time now we've been seeing seemingly random errors thrown by an application "Padding is invalid and cannot be removed". Everything I've read about this online suggests that the machine key...
0
by: Amelyan | last post by:
Why does this happen? How to fix it? Once in a while I get error in ~/ScriptResource.axd?d=... System.Reflection.TargetInvocationException: Exception has been thrown by the target of an...
4
by: MC | last post by:
Could someone please explain why I get 10-15 errors a day from my site after the "googleBot" has visited? Regards MC --------Error--------- Padding is invalid and cannot be removed....
9
by: AG | last post by:
I occassionally get the following exception from an ASP.NET 2.0 Web Application running on a shared web host. I have no way of knowing what the actual request page was as it never happens when I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.