Any C# / SQL Server experts on here? I have a rather unusual problem that Google hasn't been of much help on. I have a web app that maintains it's own tables (CREATE/DROP) in a SQL Express 2005 database. All interaction is done through stored procedures. The problem is that the stored procedure that does a "DROP TABLE <thetable>" blows up in the web interface with the following exception thrown on the web page:
Cannot drop the table 'thetable', because it does not exist or you do not have permission.
Simple problem, right? Wrong. The table in fact did get dropped. For debugging purposes, I've been running as the SA user. Also, if I run the uspDropUserTable procedure from within management studio as the SA user, the procedure runs flawlessly with no errors. The only thing I can figure is that there is some weirdness in SqlDataSources or GridViews that I'm not aware of for doing deletes. Here is the basic relevant page code being used:
<asp:GridView
ID="DocTypesGridView"
AutoGenerateColumns="false"
DataSourceID="SqlDataSource1"
DataKeyNames="TableName"
runat="server">
<Columns>
<asp:ButtonField ButtonType="Image" CommandName="Delete" ImageUrl="~/Images/delete.png" Text="Delete" />
<asp:TemplateField HeaderText="Document Type">
<ItemTemplate>
<%# Eval("FriendlyName")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:AppConnectionString %>"
SelectCommand="uspGetUserTableList"
DeleteCommandType="StoredProcedure"
DeleteCommand="uspDropUserTable"
/>
and the stored procedure:
CREATE PROCEDURE [dbo].[uspDropUserTable]
@TableName VARCHAR(250)
AS
SET NOCOUNT ON
EXECUTE('DROP TABLE [' + @TableName + ']')
Cannot drop the table 'thetable', because it does not exist or you do not have permission.
Simple problem, right? Wrong. The table in fact did get dropped. For debugging purposes, I've been running as the SA user. Also, if I run the uspDropUserTable procedure from within management studio as the SA user, the procedure runs flawlessly with no errors. The only thing I can figure is that there is some weirdness in SqlDataSources or GridViews that I'm not aware of for doing deletes. Here is the basic relevant page code being used:
<asp:GridView
ID="DocTypesGridView"
AutoGenerateColumns="false"
DataSourceID="SqlDataSource1"
DataKeyNames="TableName"
runat="server">
<Columns>
<asp:ButtonField ButtonType="Image" CommandName="Delete" ImageUrl="~/Images/delete.png" Text="Delete" />
<asp:TemplateField HeaderText="Document Type">
<ItemTemplate>
<%# Eval("FriendlyName")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:AppConnectionString %>"
SelectCommand="uspGetUserTableList"
DeleteCommandType="StoredProcedure"
DeleteCommand="uspDropUserTable"
/>
and the stored procedure:
CREATE PROCEDURE [dbo].[uspDropUserTable]
@TableName VARCHAR(250)
AS
SET NOCOUNT ON
EXECUTE('DROP TABLE [' + @TableName + ']')