DataSource Controls :: Using ObjectDataSource With Dataset, Retreive Last Inserted ID?
Jan 23, 2011
I've done a lot of searching on this here but I can't seem to find anyone who has done this. I'm sure it's a common problem but I can't figure out the right keywords to search for the solution so I'm sorry if this is a duplicate.I have a dataset where I've used the Generate Insert, Update, and Delete statements as well as the Refresh the Data Table options so that I can retrieve the last inserted ID when I'm using an ObjectDatasource.however, when I'm handling the ObjectDataSource-Inserted method, I'm getting a 1 as a return value, which I assume is the affected rows. I don't want that, I want the ID so that I can use it in my code.I looked at the generated code for the insert method and it is doing a SELECT after inserting, but it returns the whole row not the ID, so how can I get at this row to retrieve the value?
I've done a lot of searching on this her on SO but I can't seem to find anyone who has done this. I'm sure it's a common problem but I can't figure out the right keywords to search for the solution so I'm sorry if this is a duplicate.I have a dataset where I've used the Generate Insert, Update, and Delete statements as well as the Refresh the Data Table options so that I can retrieve the last inserted ID when I'm using an ObjectDatasource.however, when I'm handling the ObjectDataSource-Inserted method, I'm getting a 1 as a return value, which I assume is the affected rows. I don't want that, I want the ID so that I can use it in my code.I looked at the generated code for the insert method and it is doing a SELECT after inserting, but it returns the whole row not the ID, so how can I get at this row to retrieve the value?
I have a page with a gridview with the liste of item in relation with another item (Same table) : Select * From Item where id IN ( Select idSonItem where idParentItem = @TheItem )
When i add an item from this grid i would like to link it with the current Item : So I insert the Item with the GridView InsertMethod then I do another Query an the ItemRelation with idParentItem as the courant Item and the Inserted Item as the idSonsItem !
so the question is : How to get the id of an inserted Item after i get executed the InsertMethod ?
How do I populate a dataset with an Objectdatasource?
The reason is I have an Objectdatasource on a page which is populating a GridView ok.
I want to use a Dataset to control the DayRender of a Calendar Control and don't want to query the database again as the right date info is already in the Objectdatasource.
So can i populate a dataset with the objectdatasource ??
I have a dataset with several tableAdapters in it. For some reason, one of them that I created doesn't show up when I try to put it into my objDataSource?
I can access and use it from codebehind (testing purposes for this post.
I have a series of classes that loosely fit the following pattern:
public class CustomerInfo { public int Id {get; set;} public string Name {get; set;} public class CustomerTable { public bool Insert(CustomerInfo info) { /*...*/ } [code]...
I have an object data source control. and to insert the control I add the object to the database then call databing on the gridview the DataSource control is bound to.
My question is - how would I know where the newly added row is? I can'y go by id, because the dbase creates the id.
I'm thinking maybe last data key, but based on the sort that would be accurate.
I need to retrive the id of the inserted record to use it with another table as a FK when the user click the button this is my code that insert the record and the id in auto-increment:
[Code]....
I tried to select the id after inserting using a sqlcommand but it gave me the following error The INSERT statement conflicted with the FOREIGN KEY constraint "FK_masc_img_Mascotas". The conflict occurred in database "ads_site", table "dbo.Mascotas", column 'IDMascotas'. The statement has been terminated.
how do i copy required row in dataset to a new dataset. i have bind a xml into a dataset and i need to display say row 3 to 5 only so i do a for loop but i have encounter problem when trying to copy the rows to a new dataset.
do i have to write the xml to datatable and bind it to dataset and from there copy over ?
I am using SqlCommand to insert new value to a table. My table has 2 fileds1. IDDT (Auto increment) (numaric)2. Name (VARCHAR)i am runing below code to insert new value
[Code]....
Acording to the table the IDDT value will be inserted automatically. I need to get that value using above code.
I've been searching on the internet about this issue but all studies returns a ID value that is integer. However, in my system primary key field is a GUID, I mean uniqueidentifier. How can I get this value? Should I use SELECT SCOPE_IDENTITY() which is integer ?
I followed this BRILLIANT article and found out how to make a data layer, i just need the insert method in this code to return the ID of the record inserted.
For example, when the user captures the details, the save button is clicked, i will want to display in a label "Your Reference Number is "
try and have look at the article so you will understand how the code is, so it will not be hard to edit my code.
I am using a FormView to populate data, then inserting it using a LinqDataSource. The primary key is an autoincrementing identity. I'd like to handle the ItemInserted event to update some other tables, but I cannot find how to get the primary key of the newly inserted record. The insert happens fine, but nothing for the PK.
I realize I can change to using a SPROC, or change to creating the new row and doing the insert myself, but I'd prefer to continue with the LinqDataSource's automatic insert.
I have this image upload code...that works....but I need a code to get the Id of the image that i just uploaded..
Protected Sub ImageButton_AddImage_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton_AddImage.Click Dim IntLength As Integer Dim ArrContent As Byte()
I need to insert two rows into a tableadapter - and then take the identity column from each row and update them into a column on each row.
My problem is that I cannot update a column on a newly inserted row. Stepping through the code seems to indicate that I have updated the column, but when I check the database - the column indicates null.
I have tried calling Update on the TableAdapter, then performing the update on the column, and then calling Update again, but the update still isn't taking.
I am really having a hard time trying to write something from scratch I created a 'Person' Class.Public Class Person
Private _FirstName As String = "" Private _LastName As String = "" Private _Age As Integer = 0 Public Sub New(ByVal FirstName As String, ByVal LastName As String, ByVal Age As Integer) FirstName = FirstName _LastName = LastName _Age = Age [code]...
I have a detailsView that I use for Insert. Once a record is Inserted, I need to get the new idenetity value.
Is there some function that grabs the inserted identity value? Or do I need to query it somehow?
By the way, the reason I'm doing this is the user is inserting a record into a 'general' list that is being applied to a 'specific' list - example, an employee can select a vendor from a list, but the vendor isn't listed so the employee adds the vendor in the vedor list and while the new vendor is inserted, the vendor info is also applied to the new order record.
I have an SQL Server table named "Fields" with an autoincrement identity column "FieldID" as the primary key, an nchar column "Phrase" that textbox entries are to be stored in from a web page, and another column that is defaulted to null. The following is the recommended example on how to use Scope_Identity() to get a newly inserted table row identity value:
//C# string query = "Insert Into Categories (CategoryName) Values (@CategoryName);" + "Select Scope_Identity()"; int ID; string connect = @"Server=.SQLExpress;AttachDbFilename=|DataDirectory|Northwind.mdf;" + "Database=Northwind;Trusted_Connection=Yes;"; using (SqlConnection conn = new SqlConnection(connect)) { using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@CategoryName", Category.Text); conn.Open(); ID = (int)cmd.ExecuteScalar(); } }
Obviously, this example uses a Categories Table in a Northwind database, which I do not have. So I am confused by how to adapt this example for my application. The following is my data context:
DataClassesDataContext db = new DataClassesDataContext();
Could you explain how I can adapt the example for my "Fields" table in my "EFMDB" database?
I am using an auto-numbering index column as the primary key for many of my tables. Is there any way for me to know what the new row count is for newly inserted rows?