Click here to Skip to main content
15,887,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
public partial class ScorePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"]==null)
        {
            Response.Redirect("LoginPage.aspx");
        }
       
        Score_Page();
     
    }
    private void Score_Page()
    {
        string id = Convert.ToString(Session["UserID"]);
        string query = "SELECT UserId, SUM(AnswerResult)Score FROM t_AnswerSheet group by  UserId";
        string connection = "server=sv01;database=testdb;uid=sa;password=****";

        SqlConnection a = new SqlConnection(connection);
        a.Open();
        SqlCommand b = new SqlCommand(query, a);
        DataTable c = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(b);
        sda.Fill(c);
        a.Close();
        GridView1.DataSource = c;
        GridView1.DataBind();
    }




string id = Convert.ToString(Session["UserID"]);
       string query = "SELECT UserId, SUM(AnswerResult)Score FROM t_AnswerSheet group by  UserId";
       string connection = "server=*****;database=testdb;uid=***;password=****";






as in this part of code i am using session to get UserId from UserLoging Table but wasn't able to get User id into select command as Whenever user login it send respective id to score page along with score of that id ...but here em getting all Id and their result score. So i need just Id that get login and respective score to that Id

What I have tried:

<pre>public partial class ScorePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"]==null)
        {
            Response.Redirect("LoginPage.aspx");
        }
       
        Score_Page();
     
    }
    private void Score_Page()
    {
        string id = Convert.ToString(Session["UserID"]);
        string query = "SELECT UserId, SUM(AnswerResult)Score FROM t_AnswerSheet group by  UserId";
        string connection = "server=***;database=testdb;uid=**;password=*****";

        SqlConnection a = new SqlConnection(connection);
        a.Open();
        SqlCommand b = new SqlCommand(query, a);
        DataTable c = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(b);
        sda.Fill(c);
        a.Close();
        GridView1.DataSource = c;
        GridView1.DataBind();
    }
Posted
Updated 21-Jun-17 21:09pm
v3

1 solution

You need to add a WHERE clause to your SQL:
C#
string id = Convert.ToString(Session["UserID"]);
string query = "SELECT UserId, SUM(AnswerResult)Score FROM t_AnswerSheet WHERE UserId=@ID GROUP BY UserId";
string connection = "server=sv01;database=testdb;uid=sa;password=[REDACTED]";

using (SqlConnection con = new SqlConnection(connection))
   {
   con.Open();
   using (SqlCommand cmd = new SqlCommand(query, con))
      {
      cmd.Parameters.AddWithValue("@ID", id);
      ...


A couple of other things to make your life better:
1) Don't hardcode connection strings - always use a configuration file or similar.
2) Never post database ID and password combinations online...
3) Don't use the SA user - create users which have "just enough" permissions to do the job - it reduces the risks to the rest of your database(s), since SA can do anything at all!
4) Don't use single character variable names - it makes your code harder to read, and that means less maintainable in future (and you will spend a lot of time looking at code you or someone else has written a while a go, and working out how it works - or doesn't)
5) Always Dispose database connections, commands, adapters, etc: they are scarce resources and not doing so can make your app crash unpredictably.
 
Share this answer
 
Comments
Member 11644373 22-Jun-17 2:55am    
Thankyou ... that was helpful

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