com.ExecuteNonQuery()
always returns -1 - in all cases.
Why is it always = -1 ?
SqlConnection conn = newSqlConnection(ConfigurationManager.ConnectionStrings["UsersConnectionString"].ConnectionString);
conn.Open();
string insertquery = " select UserName from Users where Username='" + CurrentName + "' ";
SqlCommand com = new SqlCommand(insertquery, conn);
com.ExecuteNonQuery();
int ii = com.ExecuteNonQuery();
Response.Write(ii);
It's because you're using it with a SELECT
SQL command.
From the documentation for SqlCommand.ExecuteNonQuery
(emphasis mine):
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.
It's not clear what you're trying to achieve, but this basically isn't the way to do it.
See more on this question at Stackoverflow