I am writing a custom MembershipProvider for ASP.NET and would like to record certain events in a table when they happen.
I have the methods to do it, but I would like to get the value of the "ID" column of the row I insert into the table.
I have the following method (which is obviously incomplete), but I'm not sure if it will work and I'm not in a place where I can test it yet, so I thought I would ask if this appears to be a valid way of retreiving the RecordID value.
The SQL Query and the SqlCommand object are my biggest concern.
The command includes "OUTPUT Inserted.RecordID" which should work according to this. If the command is okay, then my concern is how to execute the command to read the returned value. I figured the ExecuteScalar() method is the best choice, but I wasn't completely sure what type of object is returned. Is it a row/column set or is it the literal value returned from the server where casting it as Int32 would work?
Anyway, suggestions? Comments? Ideas?
private bool EventLog(int userNumber, int userID, UserEventTypes eventType, out int recordID)
{
SqlConnection Conn = new SqlConnection(ConnectionString);
SqlCommand EventCommand = new SqlCommand("INSERT INTO EventLog " +
" (UserNumber, UserID, EventType, DateTime) OUTPUT Inserted.RecordID Values(?,?,?,?)", Conn);
EventCommand.Parameters.Add("@UserNumber", SqlDbType.Int).Value = userNumber;
EventCommand.Parameters.Add("@UserID", SqlDbType.NVarChar, 50).Value = userID;
EventCommand.Parameters.Add("@EventType", SqlDbType.NVarChar, 50).Value = eventType.ToString();
EventCommand.Parameters.Add("@DateTime", SqlDbType.DateTime).Value = DateTime.Now;
try
{
Conn.Open();
Int32 EventRecordID = (Int32) EventCommand.ExecuteScalar();
recordID = EventRecordID;
}
catch (Exception)
{
throw;
}
}
Thanks a ton! You guys are always so helpful!