Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Execption in db command..please help

Execption in db command..please help

Scheduled Pinned Locked Moved C#
helpquestiondatabaseannouncementworkspace
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    link_79
    wrote on last edited by
    #1

    Hi guys, below is a code snippet that is raising a cast exception when I attempt to pull out data from a row reader. // setup command = "SELECT IDENT_CURRENT ( @tableName )"; getLastIdentityCommand = new SqlCommand(command); getLastIdentityCommand.Parameters.Add("@tableName", SqlDbType.VarChar, 20); .... // in my prepare ftn routine , I set the connection and table name getLastIdentityCommand.Connection = con; getLastIdentityCommand.Parameters[0].Value = tableName; getLastIdentityCommand.Prepare(); ... // finally I use the damn thing elsewhere SqlDataReader rowReader = getLastIdentityCommand.ExecuteReader(); long lastInsertId = -1; // I was using 'int' before but problem still remains if (rowReader.Read()) lastInsertId = rowReader.GetInt64(0); // causes cast exception ... Things to NOTE: 1) A row is inserted into a table X with identity set - identity column is of type bigint, before the problematic db command is issued 2) I have even tried using the "SELECT @@identity" syntax in the select statement without a table name, and tried extracting the id generated as follows : int lastInsertId = (int)getLastIdentityCommand.ExecuteScalar(); 3) I have even tried rowReader.GetInt32(0); and still the same exception. Is an identity a special type and NOT and integer?? How can I get this value since I need to update a 2nd table with it. ANy and all help will be very appreciated. Thanks.

    L 1 Reply Last reply
    0
    • L link_79

      Hi guys, below is a code snippet that is raising a cast exception when I attempt to pull out data from a row reader. // setup command = "SELECT IDENT_CURRENT ( @tableName )"; getLastIdentityCommand = new SqlCommand(command); getLastIdentityCommand.Parameters.Add("@tableName", SqlDbType.VarChar, 20); .... // in my prepare ftn routine , I set the connection and table name getLastIdentityCommand.Connection = con; getLastIdentityCommand.Parameters[0].Value = tableName; getLastIdentityCommand.Prepare(); ... // finally I use the damn thing elsewhere SqlDataReader rowReader = getLastIdentityCommand.ExecuteReader(); long lastInsertId = -1; // I was using 'int' before but problem still remains if (rowReader.Read()) lastInsertId = rowReader.GetInt64(0); // causes cast exception ... Things to NOTE: 1) A row is inserted into a table X with identity set - identity column is of type bigint, before the problematic db command is issued 2) I have even tried using the "SELECT @@identity" syntax in the select statement without a table name, and tried extracting the id generated as follows : int lastInsertId = (int)getLastIdentityCommand.ExecuteScalar(); 3) I have even tried rowReader.GetInt32(0); and still the same exception. Is an identity a special type and NOT and integer?? How can I get this value since I need to update a 2nd table with it. ANy and all help will be very appreciated. Thanks.

      L Offline
      L Offline
      link_79
      wrote on last edited by
      #2

      Never mind guys, I got it - the bigint is of type decimal, I'm going to stick to int as it falls under my requirement range anyway. Don't know why I chose a damn bigint in the first place. Thanks for reading.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups