For some reason, my stored procedure runs without any error from the code in C #, but it does not delete anything that the stored procedure wrote. I have all the right parameters and that's it. I ran a query from SQL Server with all the same parameters from C # code, and it works fine. I donβt understand why this works when I run from SQL Server, but it does not work when I run it from my C # code in Visual Studio.
Here is my C # code that passes data to a stored procedure.
string reportType = "PostClaim"; string GRNBRs = "925','926','927"; string PUNBRs = "100','100','100"; string beginningDates = "20120401"; string endDates= "20120430"; try { conn = new SqlConnection(ConnectionInfo); conn.Open(); SqlDataAdapter da = new SqlDataAdapter("RemoveReport", conn); da.SelectCommand.CommandType = CommandType.StoredProcedure; da.SelectCommand.Parameters.AddWithValue("@ReportType", reportType); da.SelectCommand.Parameters.AddWithValue("@GRNBR", GRNBRs); da.SelectCommand.Parameters.AddWithValue("@PUNBR", PUNBRs); da.SelectCommand.Parameters.AddWithValue("@DATE1", beginningDates); da.SelectCommand.Parameters.AddWithValue("@DATE2", endDates); da.SelectCommand.CommandTimeout = 360; } catch (SqlException ex) {
Here is my stored procedure. It executes dynamic SQL text.
ALTER PROCEDURE [dbo].[RemoveReport] ( @ReportType NVARCHAR(20), @GRNBR VARCHAR(4000), @PUNBR VARCHAR(4000), @DATE1 DATETIME, @DATE2 DATETIME ) AS DECLARE @SQLTEXT VARCHAR(4000) BEGIN SET @SQLTEXT = 'DELETE FROM TestingTable WHERE Report=''' +@ReportType +''' AND PUNBR IN (''' +@PUNBR +''') AND [Group] IN (''' +@GRNBR +''') AND StartedAt BETWEEN '''+CONVERT(VARCHAR(10),@DATE1,121)+''' AND '''+CONVERT(VARCHAR(10),@DATE2,121)+'''' PRINT @SQLTEXT <---I'll print this out to show you what exactly it is executing. EXECUTE (@SQLTEXT) END
Here's what PRINT @SQLTEXT :
DELETE FROM MonthlyReportSchedule WHERE Report='PostClaim' AND PUNBR IN ('100','100','100') AND [Group] IN ('925','926','927') AND StartedAt BETWEEN '2012-04-01' AND '2012-04-30'
When I am in SQL Server to run this query, it works fine. But why does this not work when executed from C # code. Any help?