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

Discussion in 'ASP .Net' started by =?Utf-8?B?SGlmbmkgU2hhaHphcmQ=?=, Feb 15, 2005.

  1. 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,

    --
    Mohammed Hifni Shahzard Nazeer,
    JB Securities Pvt. Ltd.,
    Colombo.
     
    =?Utf-8?B?SGlmbmkgU2hhaHphcmQ=?=, Feb 15, 2005
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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,
    >
    > --
    > Mohammed Hifni Shahzard Nazeer,
    > JB Securities Pvt. Ltd.,
    > Colombo.
     
    William F. Robertson, Jr., Feb 15, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Harris
    Replies:
    2
    Views:
    2,703
    Simon Harris
    Apr 27, 2005
  2. Simon Harris
    Replies:
    0
    Views:
    777
    Simon Harris
    Apr 27, 2005
  3. keithb
    Replies:
    3
    Views:
    18,937
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    May 7, 2006
  4. Replies:
    20
    Views:
    9,899
    licebmi
    Sep 7, 2009
  5. =?Utf-8?B?S2luZ0xlYXI=?=

    Invalid cast from 'Int32' to 'DateTime'

    =?Utf-8?B?S2luZ0xlYXI=?=, Aug 25, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    2,005
    =?Utf-8?B?S2luZ0xlYXI=?=
    Aug 25, 2006
Loading...

Share This Page