Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Thursday, March 19, 2009

Incorrect Syntax Near sproc ...

I got this 'incorrect syntax near spoc (stored procedure)' error from the following:

string connectionString = getConnectionString("ConnectionString");
SqlConnection conn = new SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("sproc_aspnet_StocksInLists_RemoveStocksFromList", conn);

try
{
conn.Open();
command.Parameters.AddWithValue("@ListId", ddlList.SelectedValue);
iRes = command.ExecuteNonQuery();
if (iRes < 0) throw new Exception("Error removing stocks from list");
}
The bug fix is to set the command type:

string connectionString = getConnectionString("ConnectionString");
SqlConnection conn = new SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("sproc_aspnet_StocksInLists_RemoveStocksFromList", conn);
command.CommandType = System.Data.CommandType.StoredProcedure;
try
{
conn.Open();
command.Parameters.AddWithValue("@ListId", ddlList.SelectedValue);
iRes = command.ExecuteNonQuery();
if (iRes < 0) throw new Exception("Error removing stocks from list");
}

No comments: