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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FormView, Access to DataKeyNames After Insert

Status
Not open for further replies.

MdotButler

Programmer
Joined
Jan 18, 2006
Messages
104
Location
US
I have a formview which is tied to a ObjectDataSource. The datasource select is based on a querystring ("Edit"). From another page I issue command "A" as illustrated below. I also wish to access the same form via a menu passing a value (Zero as illustrated in "B" below) that allows the form to open in insert mode.
Code:
A) Response.Redirect("EditRecord_E.aspx?Edit=123")
B) Response.Redirect("EditRecord_E.aspx?Edit=0")
When working with a record that already exists I have no problems and am able to enter edit multiple times against record 123 as illustrated in example "A" above. The problem comes when accessed via a menu passing 0 to add a new record. I am able to identify this in the "Page_Load" as illustrated next.
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (IsPostBack) Then
   If FormView1.DefaultMode = FormViewMode.Insert Then
      FormView1.DefaultMode = FormViewMode.ReadOnly
   End If
Else
   If Request.QueryString("Edit") = "Add" Then
      FormView1.DefaultMode = FormViewMode.Insert
   End If
End If
End Sub
The problem occurs once I have added the record and saved it. The QueryString still exists and is then picked up in the "Page_Load" and enters insert mode again.

There has to be a better way. In case "A" I am coming from a GridView which is the result of a "Find" which works fine. In case "B", how would I obtain the new record key after insertion and reissue the access with the correct querystring? Is there a better way?

TIA
Mark

P.S. I also do not like using querystring as it gives the user a chance to modify it.
 
You can retrieve inserted keys in SQL Server by using [tt]scope_identity[/tt], and you can redirect the user to another page by using [tt]Response.Redirect[/tt].


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
I am not using stored procedures, so how can I use the scope_identity?

I agree if I can get the identity value I can issue a Response.Redirect with that value.
 
I did convert over to using stored procedures. As part of the INSERT stored procedure there is a generated select clause right after the insert command as follows:
Code:
SELECT M_MCOID, ... FROM MCO 
WHERE (M_MCOID = SCOPE_IDENTITY())
As you can see the "M_MCOID" is part of the selected data. How do I address this result set and in what method? I did try running through the dictionary as follows in the "FormView1_ItemInserted" method but the "M_MCOID" is not part of the array.
Code:
Dim aDictionary(e.Values.Count - 1) As DictionaryEntry
e.Values.CopyTo(aDictionary, 0)
Dim oEntry As DictionaryEntry
Dim cKey As Char(10)
Dim cValue As Char(10)
For Each oEntry In aDictionary
   cKey = oEntry.Key.ToString()
   cValue = oEntry.Value.ToString()
Next
If I cannot obtain the "M_MCOID" from the result set, I understand about the return value as coded next but where would I access this returned value (what method)?
Code:
Declare @NEWID int
Set @NEWID= SCOPE_IDENTITY()
Return @NEWID
GO

TIA
Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top