Monday, March 30, 2009

Dataset TableDataAdapter throws "Invalid object name" for temp tables

I added a dataset to my C# .NET project visually and tried adding a TableDataAdapter to it using a stored procedure which I had written previously. This stored procedure has a temp table #Attrition within it and the TableDataAdapter Configuration Wizard didn't seem to like this. I got an error Invalid object name "#Attrition". The solution that I figured for this is to use a @table variable instead of temp table. So I replaced

CREATE TABLE #Attrition
(
DeptID int,
Voluntary int
)


with

DECLARE @Attrition TABLE
(
DeptID int,
Voluntary int

)

and also removed the following statement at the end of the procedure

DROP TABLE #Attrition


This seems to have done the trick.