Results 1 to 3 of 3

Thread: Unable to cast object of type 'System.Int32' to type 'System.String

  1. #1

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Unable to cast object of type 'System.Int32' to type 'System.String

    This code gives me this exception saying "Unable to cast object of type 'System.Int32' to type 'System.String'." Please help me.Thanks
    Code:
    sqlcmd.CommandText=("Select count(f_activityName) from dbo.tblStandardActivities WHERE (f_activityName IS NOT NULL)");
            sread = sqlcmd.ExecuteReader();
                  
            while (sread.Read())
            {
               number_of_activities=Int32.Parse(sread.GetString(0));
             
            }
    Godwin

    Help someone else with what someone helped you!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Unable to cast object of type 'System.Int32' to type 'System.String

    There are two possible reasons.

    1. number_of_activities is a String variable.
    2. The column at index 0 contains integer data. You can only successfully call GetString if the field actually contains a string. Otherwise you should forget the parse and call GetInt32.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: Unable to cast object of type 'System.Int32' to type 'System.String

    Hi Jm,you're right,it returns an Integer from the table.....so,I used GetInt.
    Now,it works good

    Thanks
    Godwin

    Help someone else with what someone helped you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width