There are several reasons why it would be blank, first thing to make sure of is, the session variable, always check its value before doing anything.
if(Session["UserEmail"] != null) {
string id1 = Convert.ToString(Session["UserEmail"]);
} else {
}
ASP.NET has policies for sessions, plus browser also comes into action. Thus you need to check whether session has this value, or not. Further down the stream, you should also check if the the "
id
" is valid in the context or not, such as, why a condition in
SELECT
clause when it can come in the
WHERE
clause? These things would count a lot, and if things go right only then show that
DataTable
otherwise show a Label saying that the email was not found for this user;
show the id as well for debugging purposes.
Also, never concatenate SQL queries, they are exposed to
SQL Injection[
^], and no one likes injections. You code should be like,
SqlCommand com = new SqlCommand(
"SELECT UserEmail, QId, AnswerId,
CASE WHEN AnswerResult = 0 THEN
'Incorrect'
ELSE
CASE WHEN AnswerResult = 1 THEN
'Correct'
ELSE 'you have null value'
END
END
FROM t_AnswerSheet
WHERE UserId=@idparam", con);
Then again finally, pass the parameters to this command and execute it. Have a look here,
c# Using Parameters.AddWithValue in SqlDataAdapter - Stack Overflow[
^]
Using these steps, you will be able to show the
DataTable
when there is data, otherwise show a message stating that the data is not found, instead of a blank
DataTable
.