Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        
        if (Session.IsNewSession)
        {
            Response.Redirect("Login.aspx");
        }
       
        if (!Page.IsPostBack)
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["CensusConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(str);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            string @sessionId = System.Web.HttpContext.Current.Session.SessionID;
            SqlDataAdapter da = new SqlDataAdapter("select * from ureg", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow vrow in GridView1.Rows)
        {
            string card = GridView1.DataKeys[vrow.RowIndex].Value.ToString();
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["CensusConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(str);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlDataAdapter da = new SqlDataAdapter("select * from ureg where card='"+card+"'", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Session["fnm"] = dt.Rows[0]["fname"].ToString();
            Session["lnm"] = dt.Rows[0]["lname"].ToString();
            Session["gen"] = dt.Rows[0]["gender"].ToString();
            Session["cat"] = dt.Rows[0]["catagory"].ToString();
            Session["add"] = dt.Rows[0]["adr"].ToString();
            Session["dob"] = dt.Rows[0]["dob"].ToString();
            Session["ct"] = dt.Rows[0]["city"].ToString();
            Session["pin"] = dt.Rows[0]["pin"].ToString();
            Session["state"] = dt.Rows[0]["state"].ToString();
            Session["mail"] = dt.Rows[0]["mail"].ToString();
            Session["ph"] = dt.Rows[0]["phno"].ToString();
            Session["card"] = dt.Rows[0]["card"].ToString();
            Response.Redirect("update_user.aspx");
        }
    }






C#
I have this code behind file.it is showing all data of the table "ureg" after logged into the page.But i want to retrieve only the data related to the user logged in during that time.
please provide me the necessary solution to solve this problem.
Posted
Comments

Hi,

Kindly follow the below steps

Step: 1

You have to get the user id while logged in login page. then, you can store the user id on session object.

session.add("User_Id",txtUserId.Text.ToString().Trim())


Step: 2

After you logged in successfully, you can check the user_id session object is null or not condition. then, you can request database to retrieve the data for the particular user as below

if(session["User_Id"]!=null)
{
string str = System.Configuration.ConfigurationManager.ConnectionStrings["CensusConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(str);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            string @sessionId = System.Web.HttpContext.Current.Session.SessionID;
            SqlCommand cmd=new SqlCommand("Select * from ureg where user_id=@user_id", con);          cmd.Parameters.AddWithValue("@user_id",session["User_Id"].ToString().Trim());
SqlDataAdapter da=new SqlDataAdapter(cmd);         
            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
}


I hope it can be helpful to you
 
Share this answer
 
v3
Comments
Richard Deeming 23-Nov-15 9:55am    
NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
[no name] 23-Nov-15 9:58am    
Good catch and thank you
Richard Deeming 23-Nov-15 10:15am    
I've updated my vote.

PS: If you use the "Reply" button next to the comment you're replying to, the user who posted it will be notified that you've replied. :)
[no name] 23-Nov-15 10:22am    
i apologize for the my mistake. thank you for your vote
You have SQL that shows "select * from ureg". Just add a where clause on to it so you only get back the row you need.
 
Share this answer
 
During the login... Get The ID of the user by means of Session..

After That

Create a label and assign the Session Value on it

If LAble id is lbl_ureg_id mean..

lbl_ureg_id.Text=Session["userid"].ToString();



select * from ureg where uregId='"+ lbl_ureg_id.Text +"'
 
Share this answer
 
Comments

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