I need help, I have a problem while inserting a statement in SQL. I call a SQL statement from my ASP.NET program, some variables contain quotes so when the insert is fired I have an exception like:
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'xxxxx'. Unclosed quotation mark after the character string ''.
I don't want the content of my variable to be changed...
Any idea how to handle this?
The C# part :
SqlCommand cmdInsertAssessment = new SqlCommand("xxxxxxx", sqlCnx);
cmdInsertAssessment.CommandType = CommandType.StoredProcedure;
cmdInsertAssessment.Parameters.AddWithValue("@templateID", templateID);
cmdInsertAssessment.Parameters.AddWithValue("@companyID", companyID);
cmdInsertAssessment.Parameters.AddWithValue("@userID",userID);
cmdInsertAssessment.Parameters.AddWithValue("@opn",opn);
cmdInsertAssessment.Parameters.AddWithValue("@mn",Mm);
cmdInsertAssessment.Parameters.AddWithValue("@max",max);
cmdInsertAssessment.Parameters.AddWithValue("@remarque",remarque);
cmdInsertAssessment.Parameters.AddWithValue("@templateTheme",templateTheme);
cmdInsertAssessment.Parameters.AddWithValue("@name", sName);
cmdInsertAssessment.Parameters.AddWithValue("@finished", iFinished);
cmdInsertAssessment.Parameters.AddWithValue("@datenow", dtNow);
try
{
cmdInsertAssessment.ExecuteNonQuery();
}
catch (Exception e)
{
}
SQL part :
CREATE PROCEDURE ["xxxxxxx"] @templateID int,
@companyID int,
@userID int,
@opn nvarchar(255),
@mn nvarchar(255),
@max int,
@remarque nvarchar(255),
@templateTheme nvarchar(255),
@name nvarchar(255),
@finished int,
@datenow datetime
AS
BEGIN
DECLARE
@points AS FLOAT
SET @points=0
IF(@mn='M')
BEGIN
IF(@opn='O')
BEGIN
SET @points=10
END
IF(@opn='P')
BEGIN
SET @points=2
END
END
IF(@mn!='M')
BEGIN
IF(@opn='O')
BEGIN
SET @points=2
END
if(@opn='P')
BEGIN
SET @points=1
END
END
IF(@remarque=NULL)
BEGIN
SET @remarque='nothing'
END
MERGE INTO [dbo].[Assessment] as target
USING (SELECT @templateID,@companyID,@userID,@opn,@points,@max,@remarque,@templateTheme,@datenow,@name,@finished)
As source (_templateID,_companyID,_userID,_opn,_points,_max,_remarque,_templateTheme,_datenow,_name,_finished)
ON target.TemplateID=source._templateID
AND target.TemplateTheme=source._templateTheme
AND target.NameAssessment=source._name
WHEN MATCHED THEN
UPDATE SET Points = source._points, Remarque = source._remarque, FillDate= source._datenow, Finished = source._finished, OPN = source._opn
WHEN NOT MATCHED THEN
INSERT (TemplateID, CompanyID, UserID, OPN, Points, Max, Remarque, TemplateTheme, FillDate, NameAssessment,Finished)
VALUES (source._templateID,source._companyID,source._userID,source._opn,source._points,source._max,source._remarque,source._templateTheme,source._datenow,source._name,source._finished);
END
GO
Thanks :)