Line 1: Incorrect syntax near 'sp_Collect'.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QW5kcmV3?=

    Line 1: Incorrect syntax near 'sp_Collect'.

    Hi,

    What is wrong with this code ? I got this error:

    " Line 1: Incorrect syntax near 'sp_Collect'. "

    which appears at the last line when | call the cm.ExecuteScala r . The sp
    takes an int ID and returns a string Data. Using the debugger, I see that the
    value of ID is correct.
    Is this a C# problem, or SQL Server 7 problem ?

    public string GetID(int ID)
    {
    SqlCommand cm = new SqlCommand("sp_ Collect'", sqlCon);
    cm.Parameters.A dd(new SqlParameter("@ ID", ID));
    return (string) cm.ExecuteScala r();
    }

    Thanks
    Andrew
  • Pete Kane

    #2
    Re: Line 1: Incorrect syntax near 'sp_Collect'.

    Andrew wrote:
    Hi,
    >
    What is wrong with this code ? I got this error:
    >
    " Line 1: Incorrect syntax near 'sp_Collect'. "
    >
    which appears at the last line when | call the cm.ExecuteScala r . The sp
    takes an int ID and returns a string Data. Using the debugger, I see that the
    value of ID is correct.
    Is this a C# problem, or SQL Server 7 problem ?
    >
    public string GetID(int ID)
    {
    SqlCommand cm = new SqlCommand("sp_ Collect'", sqlCon);
    cm.Parameters.A dd(new SqlParameter("@ ID", ID));
    return (string) cm.ExecuteScala r();
    }
    >
    Thanks
    Andrew
    Wrap it in a try catch and output the message

    Comment

    • Marc Gravell

      #3
      Re: Line 1: Incorrect syntax near 'sp_Collect'.

      Try adding:
      cm.CommandType = CommandType.Sto redProcedure;
      (befoer the ExecuteScalar() )

      You might also need to check whether your proc *selects* the value or
      *prints* the value; ExecuteScalar() will only work for a SELECT - but
      if this was the problem I would expect a different error message.

      Marc

      Comment

      • Rene

        #4
        Re: Line 1: Incorrect syntax near 'sp_Collect'.

        Not sure if this is the problem but if you copied and paste the code from
        your project to the post, it looks like you have an extra apostrophe
        character at the end of sp_Collect.



        "Andrew" <Andrew@discuss ions.microsoft. comwrote in message
        news:7CC408CD-B883-4A66-9F24-A16911AD5290@mi crosoft.com...
        Hi,
        >
        What is wrong with this code ? I got this error:
        >
        " Line 1: Incorrect syntax near 'sp_Collect'. "
        >
        which appears at the last line when | call the cm.ExecuteScala r . The sp
        takes an int ID and returns a string Data. Using the debugger, I see that
        the
        value of ID is correct.
        Is this a C# problem, or SQL Server 7 problem ?
        >
        public string GetID(int ID)
        {
        SqlCommand cm = new SqlCommand("sp_ Collect'", sqlCon);
        cm.Parameters.A dd(new SqlParameter("@ ID", ID));
        return (string) cm.ExecuteScala r();
        }
        >
        Thanks
        Andrew

        Comment

        • =?Utf-8?B?QW5kcmV3?=

          #5
          Re: Line 1: Incorrect syntax near 'sp_Collect'.

          The line:
          cm.CommandType = CommandType.Sto redProcedure;

          did the trick.
          Thanks

          "Marc Gravell" wrote:
          Try adding:
          cm.CommandType = CommandType.Sto redProcedure;
          (befoer the ExecuteScalar() )
          >
          You might also need to check whether your proc *selects* the value or
          *prints* the value; ExecuteScalar() will only work for a SELECT - but
          if this was the problem I would expect a different error message.
          >
          Marc
          >

          Comment

          • Kelly Herald

            #6
            Re: Line 1: Incorrect syntax near 'sp_Collect'.

            First of all, you really haven't showed how the stored procedure is defined.
            Are you using a RETURN statement to return the string in the stored
            procedure? Or are you using a SELECT statement to return the string as a
            record?

            Stored procedures only return INT values. If you need a string returned you
            can use a parameter in the stored procedure with the OUTPUT tag. Short
            example below:

            CREATE PROCEDURE sp_Collect
            @ID INT,
            @ReturnValue VARCHAR(255) OUTPUT
            AS
            SELECT @ReturnValue = stringfield FROM sometable WHERE idfield = @ID
            GO

            C# code:
            public string GetID(int ID)
            {
            SqlCommand cm = new SqlCommand("sp_ Collect", sqlCon);
            cm.Parameters.A dd(new SqlParameter("@ ID", ID));
            SqlParameter paramReturnValu e = cm.Parameters.A dd("@ReturnValu e",
            SqlDbType.VarCh ar, 14);
            paramReturnValu e.Direction = ParameterDirect ion.Output;
            cm.ExecuteNonQu ery();
            return (string) paramReturnValu e;
            }




            "Andrew" <Andrew@discuss ions.microsoft. comwrote in message
            news:7CC408CD-B883-4A66-9F24-A16911AD5290@mi crosoft.com...
            Hi,
            >
            What is wrong with this code ? I got this error:
            >
            " Line 1: Incorrect syntax near 'sp_Collect'. "
            >
            which appears at the last line when | call the cm.ExecuteScala r . The sp
            takes an int ID and returns a string Data. Using the debugger, I see that
            the
            value of ID is correct.
            Is this a C# problem, or SQL Server 7 problem ?
            >
            public string GetID(int ID)
            {
            SqlCommand cm = new SqlCommand("sp_ Collect'", sqlCon);
            cm.Parameters.A dd(new SqlParameter("@ ID", ID));
            return (string) cm.ExecuteScala r();
            }
            >
            Thanks
            Andrew

            Comment

            Working...