Question

I'm using a .xsd file in my Visual Studio 2008 Pro solution and I'm having a problem with the ExecuteMode property of the tableadapter methods resetting. I have created a handful of insert methods in multiple adapters. I use "SELECT SCOPE_IDENTITY()" after the insert SQL to return the id of the new record. I then update the ExecuteMode property to Scalar (from NonQuery). Every once in a while I notice that the ExecuteMode changes back to NonQuery. I can't figure out why. I haven't noticed a pattern during my work that leads up to the change. Any of you fellow .Net geeks have any ideas?

Was it helpful?

Solution

I chalked this up as either a bug in VS or an incorrect environment setting. I couldn't consistently replicate what happened and I was unable to find a solution. It was very random and I can't remember it being a problem more than a handful of times.

OTHER TIPS

You should use a SQL that insert the values and select the new record instead of a SQL returning a scalar value to get the id.

The "TableAdapter Configuration Wizard" create that type of SQL if you choose "Create new store procedure" in "Choose a Command Type" step, then in "Enter a SQL Statement for the Select Store Procedure" step select "Advanced Options" mark "Generate Insert, Update..." and "Refresh the data table". Complete the rest of wizard with the names of the news procedures.

The SQL generated is something like this

INSERT INTO [apptbl] ([Id]) VALUES (@Id); 
SELECT Id FROM apptbl WHERE (Id = SCOPE_IDENTITY())

using the TableAdapter...

    public int insert_newObj()
    {
            objDataTable dt = new objDataTable();
            objRow dr = dt.NewObjRow();        

            // assign values to others fields, except the id...

            dt.AddObjRow(dr);

            Adapter.Update(dt);

            return dr.Id;
    }

Note that datarow object will be filled "by the Adapter" with the new id because of the second SQL statement (the SELECT...) in the store procedure.

Well, I not sure if it could help because I didn't see your SQL, but this is very common way to get the id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top