Sql inline query with parameters. Parameter is not read when the query is executed.

Posted by fzshah76 on Stack Overflow See other posts from Stack Overflow or by fzshah76
Published on 2010-04-23T19:41:40Z Indexed on 2010/04/23 19:43 UTC
Read the original article Hit count: 257

Filed under:
|

Hi All:

I am having a problem with my sql query in c#, basically it's inline query with parameters, but when I run it it tells me that parameter 1 or parameter 2 is not there

here is my query declared on top of the page as public:

public const string InsertStmtUsersTable = "insert into Users (username, password, email, userTypeID, memberID, CM7Register) " +
               "Values(@username, @password, @email, @userTypeID, @memberID,@CM7Register ); select @@identity";

this is my code for assigning the parameters, I know I am having problem so I am assigning the params twice:

Username =(cmd.Parameters["@username"].Value = row["username"].ToString()) as string; 
cmd.Parameters["@username"].Value = row["username"].ToString();

In 1 methopd it calls this query and tries to insert to table, here is the code:

Result = Convert.ToInt32(SqlHelper.ExecuteScalar(con, CommandType.Text,InsertStmtUsersTable));

Exact error message is: Must declare the variable '@username'.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET