Tuesday, March 27, 2012

@@Identity c# help...

I am trying to follow other examples I have seen on the site, and am still getting the

Must declare the scalar variable "@.@.INDENTITY".

string sqlAdd =string.Format("INSERT INTO " + siteCode +"_campaign_table (campaign_name, prod_id, type) "

+

"VALUES('{0}', '{1}', '{2}'); SELECT @.@.INDENTITY", campaignName, prodID, type);SqlCommand comAdd =newSqlCommand(sqlAdd, con);

comAdd.CommandType =

CommandType.Text;

con.Open();

//comAdd.ExecuteNonQuery();int identity;

identity =

Decimal.ToInt32((decimal)comAdd.ExecuteScalar());

lblErrorMessageAdd.Text = identity.ToString();

con.Close();

You have a typo. It should be@.@.IDENTITY

Also, your code should use parameters; what you have currently (placeholders for string replacement) is insecure and subject to SQL injection attacks.|||thanks, Corrected a few things there and works as intended.|||Actually, since you are using SQL Server, the best idea is to SELECT SCOPE_IDENTITY(). See, for example, this blog post for an explanation:SCOPE_IDENTITY() and @.@.IDENTITY Demystified. I should have pointed this out on my last post; I am sorry for missing it.

No comments:

Post a Comment