As pointed out you have two different queries in the same CommandText. When this is executed, actually both queries are executed and the 2 result sets are returned. However since you use ExecuteScalar, you only get the result from the first query.
As suggested by ChandraRam, remove the
select count
query from the beginning.
Then another important thing. Don't concatenate literal values from UI objects directly to a SQL statment. Instead always use
SqlParameter[
^]. This applies to the query but also to the insert statement.
By using parameters you have at least two benefits:
- you're more safe from SQL injections
- you don't have troubles with data type conversions (at least not so much)