Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have a page where when I log in the result page should show the table values which are stored in my result table. I have given all the codes which I've used I want such a code in my C# page that when I log in, I give my quiz and then the results are saved in my table. so now when I click on my overall result page in my, the data from the result table should come here but the thing is that the username whose signed in, only his details should be shown. so there has to be a code where the logged in users name should match with the name in result table and only his results should be shown and not others because the quiz result will save everyones data in it but I want only the result of the person whose logged in

What I have tried:

my page code
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
     <span style="font-size: large; color: #000066">Here you go
        <asp:Label ID="Label2" runat="server" Font-Names="Algerian"></asp:Label>
        , this is your final outcome and prediction of your amazing personality!</span><p>
        <asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Size="Medium" Text="Label"></asp:Label>
    </p>
<p>
                                      <span style="font-size: medium"> Result from Quiz</span></p>
<p>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Height="288px" Width="391px">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />
        </asp:GridView>
     </p>
</asp:Content>

this is c# code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PERSODICT
{
    public partial class outcome : System.Web.UI.Page
    {
        private static string cs = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["username"] != null)
            {
                Label2.Text = Session["username"].ToString();
            }
            else
            {

            }
            Label3.Text = DateTime.Now.ToString("D");


            if (!this.IsPostBack)
            {
                this.GridView1.DataSource = GetData();
                this.GridView1.DataBind();
            }

            //using (SqlConnection con = new SqlConnection(cs))
            //{
            //    con.Open();
            //    SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM quizresult", con);
            //    DataTable dtbl = new DataTable();
            //    sqlDa.Fill(dtbl);
            //    GridView1.DataSource = dtbl;
            //    GridView1.DataBind();
            //}
        }
        private DataTable GetData()
        {
            //string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            string query = "SELECT * FROM quizresult WHERE name = @Label2 ";
            SqlCommand cmd = new SqlCommand(query);
            cmd.Parameters.AddWithValue("@Label2", !string.IsNullOrEmpty(this.Page.User.Identity.Name) ? this.Page.User.Identity.Name : (object)DBNull.Value);
            using (SqlConnection con = new SqlConnection(cs))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            } 
        }
    }
}


my table data
CREATE TABLE [dbo].[quizresult] (
    [Id]          INT            IDENTITY (1, 1) NOT NULL,
    [name]        NCHAR (10)     NULL,
    [description] NVARCHAR (MAX) NULL,
    [job]         NVARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);
Posted
Updated 20-May-22 22:10pm
v2
Comments
[no name] 20-May-22 15:56pm    
Read your specs out loud; taking a breath only when you hit a period. Now put yourself in the place of someone else reading it.
Member 15198389 21-May-22 4:11am    
i have updated the q, can you check now plz, i just want a code where if statement is there and the session username is checked with the name colomn of the table and accordingy only the logged in users data is shown
DerekT-P 21-May-22 7:38am    
You're already filtering the returned data by the username; that's what "where name = @label2" is doing. So if it's not working for you, which bit is wrong? The data on the d/b, or the value returned in this.Page.User.Identity.Name? Just do normal debugging to find out what's going on.
Member 15198389 22-May-22 3:17am    
but like is the code right for shwoing only the logged in users information?dataabse has both names yet idk the data from datatable is not being shwon according to the username

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