Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have displayed UserMail related to user Id . But some time it shows value and some time it shows balnk. I have used session to fatch Email.

As i am beginner can anyone help me on this or give me any suggestions

What I have tried:

private void Score_Page()
  {

          string id1 = Convert.ToString(Session["UserEmail"]);
          int id = Convert.ToInt32(Session["UserID"]);
          string connection = ConfigurationManager.AppSettings["connection"].ToString();
          SqlConnection con = new SqlConnection(connection);
          con.Open();
          SqlCommand com = new SqlCommand("SELECT  UserEmail ='" + id1 + "',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='" + id + "'", con);
          SqlDataAdapter sda = new SqlDataAdapter(com);
          DataTable dt = new DataTable();
          sda.Fill(dt);
          con.Close();
          GridView1.DataSource = dt;
          GridView1.DataBind();
   }
Posted
Updated 28-Jun-17 3:16am

1 solution

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.
C#
if(Session["UserEmail"] != null) {
   string id1 = Convert.ToString(Session["UserEmail"]);
   // Continue
} else {
   // Do not load anything, value is empty.
}

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,
C#
// I removed the = '' from UserEmail in SELECT, add if needed; check CASE blocks too.
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900