I have written a stored procedure that returns XML. When I run the stored procedure in SQL Server management studio, it returns the xml as it should but, when I run it in my C# code, no xml is returned. My code looks like this:
using (SqlConnection conn = new SqlConnection(connStr)) {
using (SqlCommand cmnd = new SqlCommand("dbo.spMyStoredProcXML", conn)) {
cmnd.CommandType = CommandType.StoredProcedure;
cmnd.Parameters.Add("@Param1", SqlDbType.VarChar, 50);
cmnd.Parameters["@Param1"].Value = "Some value";
//more parameters...
conn.Open();
XmlReader xrdr = cmnd.ExecuteXmlReader();
//do stuff...
}
}
I have checked the permissions for the user that I created, and they look like this: At the server level, in the Login Properties dialog, under User Mapping, I have mapped the user to the correct database, and "checked" the public, db_datareader, db_datawriter, and then even added them to db_owner roles.
At the database level, I have also gone into the "Securables" section of the Database User dialog and explicitly given the user Grant and With Grant permissions to Execute the stored procedure in question. I have also explicitly selected "Grant" permissions for the user to Delete, Insert, Select, and Update all the tables in the database.
Still, when the "XmlReader xrdr = cmnd.ExecuteXmlReader();" line runs, the xrdr variable is empty. No error is reported. Can someone give suggestions as to what else I need to examine?
Thanks in advance for any help you can provide.
Here is a simplified version of the stored procedure (which works perfectly in SQL Server Management Studio):
CREATE PROCEDURE [dbo].[spGetXML]
(@param1 varchar(50), @param2 varchar(1))
AS BEGIN
SET NOCOUNT ON;
with xmlnamespaces ('http://schemas.xmlsoap.org/soap/encoding/' as SOAP_ENC)
select
d.Col1,
d.Col2,
from MyTable d
where d.Col1 = @param1 and
d.Col2 = @param2
FOR XML PATH('ObjectName'), root('DOCUMENT'), type
END