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 8811721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:26:07+00:00 2026-06-14T03:26:07+00:00

When I try to run this i get an SqlException with the message Incorrect

  • 0

When I try to run this i get an SqlException with the message “Incorrect syntax near the keyword ‘PROCEDURE’.”

120: cmd.CommandText = @"CREATE PROCEDURE FindState @Name nvarchar(50)
     AS SELECT IDState FROM State WHERE Name=@Name";
121: cmd.ExecuteNonQuery();

Stack trace:

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at xxx.Program.Main(String[] args) in C:\xxx\Program.cs:line 121

However I tried copying the SQL command directly in a SQL query and executing it and it worked. (it created the procedure)

To give you some context here is a little bigger section of the code

SqlParameter stateName = new SqlParameter();
stateName.SqlDbType = SqlDbType.NVarChar;
stateName.Direction = ParameterDirection.Input;
stateName.ParameterName = "@Name";
stateName.Size = 50;
Console.Write("Enter state name: ");
stateName.Value = Console.ReadLine();
cmd.Parameters.Add(stateName);

cmd.CommandText = @"IF EXISTS (SELECT name FROM sysobjects WHERE name='FindState' AND type='P') DROP PROCEDURE FindState";
cmd.ExecuteNonQuery();
cmd.CommandText = @"CREATE PROCEDURE FindState
@Name nvarchar(50)
AS
SELECT IDState FROM State WHERE Name=@Name";
cmd.ExecuteNonQuery();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "FindState";
sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
  while (sdr.Read())
  {
    Console.WriteLine("ID state " + stateName.Value + " is " + sdr[0]);
  }
}
else
{
  Console.WriteLine("No such state exists");
}
sdr.Close();
  • 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-06-14T03:26:08+00:00Added an answer on June 14, 2026 at 3:26 am

    I figured out what the problem was.
    The parameter was added to the SqlCommand before creating the procedure.
    I moved this line down and now it works

    cmd.CommandText = @"CREATE PROCEDURE FindState @Name nvarchar(50) AS SELECT IDState FROM State WHERE Name=@Name";
    cmd.ExecuteNonQuery();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "FindState";
    cmd.Parameters.Add(stateName);
    sdr = cmd.ExecuteReader();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to run this code, I get incorrect maximums and minimums. Could
I get this error FB is not defined when i try to run this
I get this, when I try to run my PHP. When I comment out
Whenever i try to run sqlmetal, i get this: 'sqlmetal' is not recognized as
When I try and run django, this is what I get: /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python manage.py runserver
This Java code compiles fine, but when I try to run it I get:
If I try to run this query in SQL Server 2005: SELECT 1 WHERE
I have the given view and template code, when i try to run this
I'm getting this error when I try run DB:Rake : ** Invoke db:migrate (first_time)
i have this thread: public void run() { try { while(isThereActivityRunning()) { results =

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.