473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.ExecuteScalar . 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.Add(new SqlParameter("@ID", ID));
return (string) cm.ExecuteScalar();
}

Thanks
Andrew
Feb 13 '08 #1
5 4477
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.ExecuteScalar . 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.Add(new SqlParameter("@ID", ID));
return (string) cm.ExecuteScalar();
}

Thanks
Andrew
Wrap it in a try catch and output the message
Feb 13 '08 #2
Try adding:
cm.CommandType = CommandType.StoredProcedure;
(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
Feb 13 '08 #3
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" <An****@discussions.microsoft.comwrote in message
news:7C**********************************@microsof t.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.ExecuteScalar . 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.Add(new SqlParameter("@ID", ID));
return (string) cm.ExecuteScalar();
}

Thanks
Andrew

Feb 13 '08 #4
The line:
cm.CommandType = CommandType.StoredProcedure;

did the trick.
Thanks

"Marc Gravell" wrote:
Try adding:
cm.CommandType = CommandType.StoredProcedure;
(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
Feb 13 '08 #5
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.Add(new SqlParameter("@ID", ID));
SqlParameter paramReturnValue = cm.Parameters.Add("@ReturnValue",
SqlDbType.VarChar, 14);
paramReturnValue.Direction = ParameterDirection.Output;
cm.ExecuteNonQuery();
return (string) paramReturnValue;
}


"Andrew" <An****@discussions.microsoft.comwrote in message
news:7C**********************************@microsof t.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.ExecuteScalar . 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.Add(new SqlParameter("@ID", ID));
return (string) cm.ExecuteScalar();
}

Thanks
Andrew

Feb 15 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: samuel | last post by:
Hi I wrote a macro in MS Access 2000. This is a .adp link on SQL server tables. I have 2 forms: script_selection & Form3 Form3 include 2 important fields: CD & line I want to write a...
2
by: Roger R. Smith | last post by:
I see this error in my ASP .NET code: I am trying to execute the following: private const string SQL_SELECT_USER = "SELECT User.Email, User.FirstName, User.LastName, User.Status, User.Phone,...
1
by: bdastani | last post by:
I am trying to initialize a dataReader in C#, but I keep on gettin an exception error stating the following: System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior,...
3
by: Patrick.O.Ige | last post by:
Hi guys, I'm getting error:- System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near '='. What could cause this ERRORR My code below:- Dim connString As String =...
1
by: Sandesh | last post by:
Hello All, Me saying " has any body come across such error would be underestimating". Well I am getting a very peculiar and unique error "Line 1: Incorrect syntax near 'Actions'." ...
6
by: martin1 | last post by:
I just use DataSet to bind DataSetGrid and display from SQL DB. when starting run in Visual Studio 2005, get "Line 1: Incorrect syntax near '1'" error message from below fill line, ...
1
by: iporter | last post by:
In the following code, the two Response.Write statements output exactly the same - I can copy and paste both into Query Analyzer, and run them fine. However, if I comment out line 3, the...
1
by: Ruth413 | last post by:
Hello - I am getting the these errors in my sql 2005 generated script. I am stuck with first error. Please help.... Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '('. Msg 319,...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.