views:

75

answers:

1

I am calling a stored procedure on sql server like this:

SqlConnection conn = new SqlConnection();
SqlCommand cmd;
XmlDocument xmlDocument;
XmlReader xr;
XmlNode node;
SqlDataReader rdr = null;

try
{
    xmlDocument = new XmlDocument();
    conn.ConnectionString = "Data Source=test;Initial Catalog=teste;Integrated Security=SSPI;";
    cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "[dbo].[spSearchKeywords]";

    cmd.Parameters.Add(new SqlParameter("@VALUE", "XPT"));

    conn.Open();

    xr = cmd.ExecuteXmlReader();

    conn.Close();
    node = xmlDocument.ReadNode(xr);
}

its connecting and executing the command, however it returns nothing there is data to return and the parameters are correct(when I call the procedure in sql with the same parameter it returns me a result)

here is the proc:

ALTER PROCEDURE [dbo].[spSearchKeywords]
(
    @VALUE                 NVARCHAR(50)  = NULL,
    @ACCOUNTGROUPID         NVARCHAR(50)  = NULL,
    @ShortCodeId         NVARCHAR(50)  = NULL,
    @VALUETYPE             NVARCHAR(50)  = NULL,
    @ASSEMBLY             NVARCHAR(100) = NULL,
    @ASSEMBLYCONTAINSURI NCHAR   (10)  = NULL,
    @ASSEMBLYTYPE         NVARCHAR(50)  = NULL
)
AS
BEGIN

    SET NOCOUNT ON;

    SELECT [Value]
          ,[AccountGroupId]
          ,[ShortCodeId]
          ,[ValueType]
          ,[assembly]
          ,[assemblyContainsUri]
          ,[assemblyType]
      FROM [teste].[dbo].[keywords]
     WHERE [Value]                 = ISNULL(@VALUE,                [Value])
       AND [AccountGroupId]         = ISNULL(@ACCOUNTGROUPID,        [AccountGroupId])
       AND [ShortCodeId]         = ISNULL(@SHORTCODEID,            [ShortCodeId])
       AND [ValueType]             = ISNULL(@VALUETYPE,            [ValueType])
       AND [assembly]             = ISNULL(@ASSEMBLY,            [assembly])
       AND [assemblyContainsUri] = ISNULL(@ASSEMBLYCONTAINSURI, [assemblyContainsUri])
       AND [assemblyType]         = ISNULL(@ASSEMBLYTYPE,        [assemblyType])
     FOR XML AUTO
END
+8  A: 

You can't close your connection before actually using the XmlReader. Try dropping the conn.Close() below node = xmlDocument.ReadNode(xr);. And consider the using statement for your disposable database objects.

AJ
I was thinking along those lines as well. Surprised there's no exception though in that case.
Kirk Woll
lol, I knew it was something really idiot, thanks man!
yuneyev
@Kirk Woll: What exception would you throw?
Chris Lively
@Chris, I wouldn't throw an exception. I just would have assumed that if the result of ExecuteXmlReader is dependent on an open SQL connection, and that connection is closed first, that an exception would have been thrown by the framework due to that fact.
Kirk Woll