Solved

Returning a value From SQL

Posted on 2006-11-12
6
247 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
  • 3
  • 3
6 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 500 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 29

Expert Comment

by:Nightman
ID: 17926759
0
 

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
 

Author Comment

by:Scripter25
ID: 17926877
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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