?
Solved

Returning a value From SQL

Posted on 2006-11-12
6
Medium Priority
?
262 Views
Last Modified: 2008-03-06
ok here is my code which as you will see does not work,
All I need is to store the record count into a variable I just dont know how to do that with ASP.net

            string QiD = (string)Request.QueryString["testid"];
            SqlDataReader dtr;
            SqlConnection oConn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\quiz.mdf;Integrated Security=True;User Instance=True");
            SqlCommand cmd = new SqlCommand("usp_Question_Count", oConn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter workParam = new SqlParameter("@QuizId", QiD);
            workParam.Value = QiD;
            cmd.Parameters.Add(workParam);
            // With...
           
            oConn.Open();
            dtr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            // get the total number of products
            int iReturn = Convert.ToInt32(cmd.Parameters["Count"].Value);
           
            int testID = int.Parse(Request.QueryString["testid"]);


and here is my Stored Procedure

ALTER PROCEDURE dbo.usp_Question_Count
     @QuizId int
     /*@QuestionCount int OUTPUT
     
     (
     @parameter1 int = 5,
     @parameter2 datatype OUTPUT
     )
     */
     
AS
DECLARE @Count AS int
     /* SET NOCOUNT ON */
     /*DECLARE @Count AS int*/
     SELECT @Count=COUNT(QuestionOrder) FROM Question WHERE (QuizID = @QuizId)
    /* SET @QuestionCount = @Count*/
      RETURN @Count


0
Comment
Question by:Scripter25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 2000 total points
ID: 17926712
Your stored proc looked fine (cleaned up a little)

ALTER PROCEDURE dbo.usp_Question_Count
     @QuizId int
     @QuestionCount int OUTPUT
   
AS
 SELECT @QuestionCount=COUNT(QuestionOrder) FROM Question WHERE (QuizID = @QuizId)


BUT what you want is to run ExecuteNonQuery (you don't really need a data reader here, it's an overhead) and you need to define the @QuestionCount parameter as output - then you can query it (bear with me - I don't have a compiler with me to check syntax, so this is all from memory):

            string QiD = (string)Request.QueryString["testid"];
            SqlConnection oConn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\quiz.mdf;Integrated Security=True;User Instance=True");
            SqlCommand cmd = new SqlCommand("usp_Question_Count", oConn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter workParam = new SqlParameter("@QuizId", SqlDbType.Int);
            workParam.Value = QiD;
            cmd.Parameters.Add(workParam);
             SqlParameter countParam = new SqlParameter("@QuestionCount", SqlDbType.Int);
            countParam.Direction=ParameterDirection.Output
            cmd.Parameters.Add(workParam);
            cmd.Parameters.Add(countParam);
     
            oConn.Open();
            cmd.ExecuteNonQuery();
            // get the total number of products
            int iReturn = Convert.ToInt32(cmd.Parameters["QuestionCount"].Value);
           oConn.Close();
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17926730
You were on the right track - there is no need for a datareader or a dataset if you can simply return an output parameter - and it's far more efficient (less load on SQL, less load on the network, less load in memory and CPU on the web server).

Don't forget to put a try catch finally around this and make sure that the oConn.Close() method happens there (SQL connections use pooling by default, and DON'T get implicitly closed. There may be an overload for the ExecuteNonQuery() to do CommandBehavior.CloseConnection, but wihtout the ompiler I can't check ;)

You can find some good examples at:

http://www.codersource.net/microsoft_data_access_application_block_azam.html
http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=624
and the MSDN
0
 

Author Comment

by:Scripter25
ID: 17926747
You are the man I only had to fix two very small things in your script above

 //cmd.Parameters.Add(workParam); It gave an error
also
I had to put the @ on
int iReturn = Convert.ToInt32(cmd.Parameters["@QuestionCount"].Value);


Hey hope you are going to be on for awhile tonight I am trying to get this part of this project done tonight and will probably have atleast 1 if not two or 3 500 point questions coming through
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Scripter25
ID: 17926820
Yeah the last one has been marked to be removed for sometime now I just posted in the community support area for a cleanup of that and one other one.


Thank you again
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question