According to stack trace, the error is in
vnetwork.Account.DatabaseEntry.Page_Load
method when you are trying to get data from reader. The code you have posted is kind of irrelevant. You should check this method where you are probably trying to get data from the column that is not returned by the query.
Update:
The error is because either there is no column called UserName returned from your query or there is no data returned from the query. A better way to get data from reader is:
if (reader.HasRows)
{
while (reader.Read())
{
}
}
Now, there are few issues with the code you have here. It is not really related to the question. These may be really irrelevant in case the code posted is just for sample.
1. Looks like you already have
UserName
in
Session
object. So why are you even running this query.
2. In case the user name in database is something else, you will need to use query. Now, if you only need the name, your SQL command should return on the name column.
3. Cache the user name in memory using a suitable method. This way you do not need to run this query on every postback.
4. You should ensure to close the
SqlConnection
. Take a look at
using
code block. It will come handy.
5. Make use of paramterized queries rather than using string concatenation.