N
nsj
I am working with a web applicaction that accesses a SQL Server database. I
need the value of the 'id' column of the last inserted row in the table
'PERSON'. The SQL statement for that purpose is: "SELECT
IDENT_CURRENT('PERSON')"; . I need to insert this value in another table
called 'ADDRESS'. So I did the following:
System.Data.IDbConnection dbConnection = new
System.Data.SqlClient.SqlConnection(connectionString);
string queryString = "INSERT INTO [ADDRESS] ([id]) VALUES (@id)";
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
System.Data.IDataParameter dbParam_id = new
System.Data.SqlClient.SqlParameter();
dbParam_id.ParameterName = "@id";
dbParam_id.Value = "SELECT IDENT_CURRENT('PERSON')";
dbParam_id.DbType = System.Data.DbType.Int64;
dbCommand.Parameters.Add(dbParam_id);
I get the following error:
Exception Details: System.FormatException: Input string was not in a correct
format.
PERSON.id datatype is "bigint" in Sql server.
ADDRESS.id datatype is "bigint" in Sql server.
need the value of the 'id' column of the last inserted row in the table
'PERSON'. The SQL statement for that purpose is: "SELECT
IDENT_CURRENT('PERSON')"; . I need to insert this value in another table
called 'ADDRESS'. So I did the following:
System.Data.IDbConnection dbConnection = new
System.Data.SqlClient.SqlConnection(connectionString);
string queryString = "INSERT INTO [ADDRESS] ([id]) VALUES (@id)";
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
System.Data.IDataParameter dbParam_id = new
System.Data.SqlClient.SqlParameter();
dbParam_id.ParameterName = "@id";
dbParam_id.Value = "SELECT IDENT_CURRENT('PERSON')";
dbParam_id.DbType = System.Data.DbType.Int64;
dbCommand.Parameters.Add(dbParam_id);
I get the following error:
Exception Details: System.FormatException: Input string was not in a correct
format.
PERSON.id datatype is "bigint" in Sql server.
ADDRESS.id datatype is "bigint" in Sql server.