473,378 Members | 1,623 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,378 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 4810
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.