Solved

Pass Parameter to Stored Procedure

Posted on 2013-06-10
3
521 Views
Last Modified: 2013-06-10
I need to pass a parameter to a stored procedure from code.

This is the stored procedure:

ALTER PROCEDURE [dbo].[usp_getPriceChart]
      -- Add the parameters for the stored procedure here
AS
DECLARE      @SymbolID AS integer

BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

SELECT QuoteDate, ClosePrice
      FROM StockHist
    WHERE SymbolID=@SymbolID AND QuoteDate <= GETDATE()
      AND QuoteDate >= DATEADD(day,-65,GETDATE())
    ORDER BY QuoteDate

      
      END

Here is the c# code:

Int16 intSymbolID = 6939;

 SqlCommand cmdGetChartData = new SqlCommand();
            cmdGetChartData.CommandType = CommandType.StoredProcedure;
            cmdGetChartData.CommandText = "usp_getPriceChart";

            // Add the input parameter and set its properties.
            SqlParameter parameter = new SqlParameter();
            parameter.ParameterName = "@SymbolID";
            parameter.SqlDbType = SqlDbType.Int;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = intSymbolID;
            cmdGetChartData.Parameters.Add(parameter);

            dscWatchListChart.SelectCommand = cmdGetChartData.CommandText;  

dscWatchListChart.DataBind();  // Data is not displayed.

Works when the stored procedure is hard coded.
SET @SymbolID = 6939

What should I change?

Thanks
0
Comment
Question by:Dovberman
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 300 total points
Comment Utility
Hi,
please change the beginning of your stored procedure from
ALTER PROCEDURE [dbo].[usp_getPriceChart]
      -- Add the parameters for the stored procedure here
AS
DECLARE      @SymbolID AS integer

BEGIN

Open in new window

to
ALTER PROCEDURE [dbo].[usp_getPriceChart]
   @SymbolID AS integer
      -- Add the parameters for the stored procedure here
AS
BEGIN

Open in new window


You have to move the variable right after the procedure name.

HTH
Rainer
0
 

Author Comment

by:Dovberman
Comment Utility
Thank you.
0
 

Author Closing Comment

by:Dovberman
Comment Utility
Thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now