Execption in db command..please help
-
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.
-
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.