Getting 'Autonumber' Generated by Smart Object 'Create'

  • 23 October 2008
  • 1 reply
  • 1 view

Badge +9

When using the Smart Objects Client API to 'create' an instance of a smart object, is there any way to get the 'autonumber' value that was generated as the key all in one call?  I've tried to define the autonumber column as a return parameter or an output parameter, but it is always null.  I'm not sure how the actual insert works under the covers, so I don't know if it even returns the generated id.  Here is a code snippet:


SourceCode.Data.SmartObjectsClient.

SOCommand cmd = cnn.CreateCommand();


cmd.CommandType =

CommandType.StoredProcedure;


cmd.CommandText =

"[Work_Item.Create]";


cmd.Parameters.Add(

new SourceCode.Data.SmartObjectsClient.SOParameter("Work Item Id",DbType.Int32,999,ParameterDirection.Output,true,"Work_Item_Id",DataRowVersion.Current,null)); // This is an auto-generated number


cmd.Parameters.Add(

new SourceCode.Data.SmartObjectsClient.SOParameter("Client_Services_Request_Id", 3254)); // one input parameter


cmd.ExecuteNonQuery();


System.Diagnostics.

Debug.Write(cmd.Parameters["Work Item Id"].Value); // Always null


 


1 reply

Badge +9

Ok, I posted before I thought it through (typical).  Apparently, the 'Create' method returns one row one column, with the new identifier so using an execute scalar will return the new id:


 cmd = cnn.CreateCommand();


cmd.CommandType = System.Data.CommandType.StoredProcedure;


cmd.CommandText = "[Work_Item.Create]";


cmd.Parameters.Add(new SourceCode.Data.SmartObjectsClient.SOParameter("Client Services Request Id", clientServicesRequestId));


cmd.Parameters.Add(new SourceCode.Data.SmartObjectsClient.SOParameter("Service Type", "Communication"));


newWorkItemId = (int)cmd.ExecuteScalar();

Reply