Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get PrimaryKey value of FormView

Status
Not open for further replies.

IT4EVR

Programmer
Feb 15, 2006
462
US
I'm inserting a new record into the database via a FormView. I'm not displaying the system generated ID number. How am I able to get this value programmatically?

I'm using a FormView control bound to an objectdatasource control. I've specified a DataKeyName in the FormView.
 
I want to retrieve this value right after the insert.
 
YOu will have to pass back the value from a stored procedure as an OutPut param.
 
I've created the OUT Parameter in the stored procedure, but not sure where or how to access this in code since I am using the ObjectDataSource.
 
What I ended up doing is deriving the max id within the stored procedure and used it to take the action I needed.

I still would like to learn how to do this on the client side with the formview.

I tried various things on the datasource_Inserted and the FormView_Inserted events but I only generated errors.
 
Code:
What I ended up doing is deriving the max id within the stored procedure and used it to take the action I needed

Bad Idea.. If you have multiple users hitting the db and inserting, using max could return the wrong value.
 
Well, it will work for this case because it's a table that only one person will be adding records to. I consider it a temporary solution until I figure this one out.
 
If you are using the SP to get the max ID, why not just use SCOPE_IDENTITY and you won't ever have a problem?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Actually I am using Oracle so that doesn't apply. With Oracle you define a sequence object that defines the seed and increment for a table. I can do a seq.nextval to insert a new id.

I know how to return an output parameter in code with the command object, however, this is an object data source and the formview is using this class internally. In the web form code I am not calling the methods, it's doing it through the formview.

That's why I was looking for a way to get the inserted id through the formview.
 
Actually I am using Oracle so that doesn't apply
OK, but I'm sure you can do the same in an Oracle stored procedure by using a returning clause.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Right, and I used that when I wasn't implementing the formview. When not implementing the formview, you are calling the data access methods directly in code, so it's easy to return a value.

But in a form view, the control itself is accessing the data access code. I tried putting an output parameter in the form view but it didn't seem to access this returned value.

I've seen some examples where you can get the newly inserted value in the datasource_inserted event but it didn't seem I could do it.

In a nutshell, I have one procedure that inserts a new record. Then I want to grab that new id and use that to insert that value with others into another table. I have a multi-select list box on the form, and based on the user selections, I have to insert or update values in another table. You can bind the items in the listbox but you really can't bind the selected items. You have to generate that through code.
 
When not implementing the formview, you are calling the data access methods directly in code, so it's easy to return a value.

But in a form view, the control itself is accessing the data access code.
Why is a FormView any different to any other controls that allow you to view/edit/insert records? It still has DataSource and DataBind events so you can still set these via code as you would with any other control.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
The solution I decided to use was to insert the records into both tables in the same operation (stored procedure). I found a way to send an array of values (VARRAY) to an Oracle procedure. This array constitutes one or more selected values from the multi select list box.

ODP.NET provides functionality for sending arrays, collections, from the client to the Oracle procedure.

This seems to me a better approach because it offers less calls across the network and less database calls.
 
While I was able to find another way to do this, I was never able to find a resolution to why the ObjectDataSource didn't want to send the OutputParameter back to the client. I'm using the right events (ObjectDataSource_Inserted) and the right code.

In my research I found that the ObjectDataSource has a slew of problems, especially when dealing with Oracle. It may well be a datatype issue. It may work better in tandem with SqlServer.

Other people more knowledgeable than I have already pointed out its limitations.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top