views:

415

answers:

2

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?

A: 

I have check this issue, When you set mode to Scaler, after that if you modify in query, it will then change mode to again NonQuery.

Muhammad Akhtar
This sounds like it may be a bug in Visual Studio
Brian
might be, But I think, after modification in query mode will be set automatically according to query
Muhammad Akhtar
A: 

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.

antonio