troubleshooting Question

Subquery results in SP getting lost when called in .Net App

Avatar of Dominic Lennon
Dominic LennonFlag for United Kingdom of Great Britain and Northern Ireland asked on
Microsoft SQL ServerASP.NET
3 Comments1 Solution406 ViewsLast Modified:
Hi,

I've written a Stored Procedure below:-

CREATE PROCEDURE [dbo].[getQuestionsByTopic]
      -- Add the parameters for the stored procedure here
      @topicId int,
      @quoteId int
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for procedure here
      SELECT q.*, a.questionAnswer, a.answerId FROM question q
      LEFT JOIN (SELECT questionId, questionAnswer, quoteId, Id AS answerId FROM questionAnswer WHERE quoteId = @quoteId) AS a ON q.questionId = a.questionId
      WHERE q.topicId = @topicId;
END

When ran from SQL Management Studio, it returns what I want - all questions and any answers based on Quote ID. However, when I call it in my .Net app, the fields specifically in the subquery never appear. Any ideas? Here's my code which grabs a dataset anyway:-

public DataTable getQuestionsByTopic (int topicId, int quoteId) {
                  DataTable dt = new DataTable();
                  SqlConnection conn = new SqlConnection();
                  
                  Open(conn);
                        using (SqlCommand command = new SqlCommand()) {
                              command.Connection = conn;
                              command.CommandType = CommandType.StoredProcedure;
                              command.CommandText = "getQuestionsByTopic";
                    command.Parameters.AddWithValue("@quoteId", topicId);
                    command.Parameters.AddWithValue("@topicId", topicId);

                              try { using(SqlDataAdapter adapt = new SqlDataAdapter(command)) adapt.Fill(dt); }
                              catch (Exception ex) { WritetoEventLog(ex.Message); }
                              finally { conn.Close(); }
                        }
                  
                  return dt;
            }
ASKER CERTIFIED SOLUTION
BuggyCoder

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros