Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:54:43+00:00 2026-05-25T18:54:43+00:00

Hello I’m having trouble displaying the output of a SP in a console window

  • 0

Hello I’m having trouble displaying the output of a SP in a console window using C#. Here is the SP code first then the C# after that.

(SP code (Updated with SP code))

- GetDepartmentName stored procedure.
 IF NOT EXISTS (SELECT * FROM sys.objects 
        WHERE object_id = OBJECT_ID(N'[dbo].[GetDepartmentName]') 
        AND type in (N'P', N'PC'))
 BEGIN
 EXEC dbo.sp_executesql @statement = N'
 CREATE PROCEDURE [dbo].[GetDepartmentName]
  @ID int,
  @Name nvarchar(50) OUTPUT
  AS
  SELECT @Name = Name FROM Department
  WHERE DepartmentID = @ID
   '
   END
   GO

(c# code)

     public void RunStoredProcParams()
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;

            int ID = 1;
            //string Name = ""; Tried testing with this 

            try
            {
                conn = new
                SqlConnection("Server=(local);DataBase=School;Integrated Security=SSPI");
                conn.Open();

                SqlCommand cmd = new SqlCommand("GetDepartmentName", conn);

                cmd.CommandType = CommandType.StoredProcedure;

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

                //cmd.Parameters.Add(new SqlParameter("@Name", Name));  Tried testing 
    //with this. Don't get output when added, get error message when commented out.

                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    Console.WriteLine("Department: {0}", rdr["Name"]);

                }

            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
        }

    }


 //cmd.Parameters.Add(new SqlParameter("@Name", Name)); 

I added this line above because I keep getting error message “”Procedure or function ‘GetDepartmentName’ expects parameter ‘@Name’, which was not supplied.” Without an input, I get an error message even though this is suppose to output the results..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:54:43+00:00Added an answer on May 25, 2026 at 6:54 pm

    You have to add that parameter to the command like you did with id

    cmd.Parameters.Add(new SqlParameter("@Name"){Direction = Output});
    

    After you execute the command, you can get the value from

    cmd.Parameters["@Name"]
    

    Ok try this then

    SqlConnection con = new SqlConnection("ConnectionString");
            SqlCommand cmd = new SqlCommand("GetDepartmentName", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int) { Value = _ID });
            cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar) { Size = 50, Direction = ParameterDirection.Output });
            con.Open();
            cmd.ExecuteNonQuery();
    
        _Name = cmd.Parameters["@Nume"].Value.ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i'm having some trouble in displaying multiple pushpins on maps. So far i've
Hello Stackoverflowers, I'm trying to use a button, that first goes to another excel
Hello I am trying to do a SOAP request here is my code: #
Hello i have some simplified code here showing the problem i have. Basically i
Hello, I want to put custom status bar in notification window of iphone. is
Hello guys I need to pass a string array over to a setter that
Hello , I am using arshaw fullcalendar v1.5.2 (only month view ) for property
Hello I have this problem with PyQt4-dev-tools that include: * a user interface compiler
Hello i have this code var queue = new BlockingCollection<int>(); queue.Add(0); var producers =
Hello I want to know how to highlight all Words that is inputted in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.