specified cast is not valid error in executescalar.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • trialproduct2004@yahoo.com

    specified cast is not valid error in executescalar.

    hi all
    i am having application which is connecting to databset and executing
    one query as below:-

    string str;
    str="select count(*) from table1;

    and then i am using sqlcommand to get result as below:-

    long l=(long)sqlcomm and.ExecuteScal ar();

    at this line my code is giving error 'specified cast is not a valid'.

    but if i am collecting result of executescalar in string then it is
    working properly.

    can some one tell me why this is happending.

    Any help will be appreciated

    thanks in advance.

  • buddyjoemama@gmail.com

    #2
    Re: specified cast is not valid error in executescalar.

    Use:

    long l = Convert.ToLong( sqlCommand.Exec uteScalar())

    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: specified cast is not valid error in executescalar.

      Hi,

      The return of ExecuteScalar is object , you are casting it to a long and
      getting an exception , the reason:
      the value you are getting is not castable to long

      Solution:
      use Convert.ToLong ( )

      IIRC you are getting a SqInt32 struct which is castable to Int32

      cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation


      <trialproduct20 04@yahoo.com> wrote in message
      news:1131354493 .091571.321370@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > hi all
      > i am having application which is connecting to databset and executing
      > one query as below:-
      >
      > string str;
      > str="select count(*) from table1;
      >
      > and then i am using sqlcommand to get result as below:-
      >
      > long l=(long)sqlcomm and.ExecuteScal ar();
      >
      > at this line my code is giving error 'specified cast is not a valid'.
      >
      > but if i am collecting result of executescalar in string then it is
      > working properly.
      >
      > can some one tell me why this is happending.
      >
      > Any help will be appreciated
      >
      > thanks in advance.
      >[/color]


      Comment

      Working...