Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two module in my website jobseeker and recruiter.
I have to display lastlogin date in each page of 2 modules.

my login code is

C#
protected void btnlogin_Click(object sender, EventArgs e)
    {


        string q = "select count(*) from logdate where eid = '" + txtuname.Text.Trim() + "' and password = '" + txtpasswd.Text.Trim() + "'" ;
        cmd = new SqlCommand(q, cnn);
        object obj;
        obj = cmd.ExecuteScalar();
        if (obj.ToString() == "1")
        {
            cmd = new SqlCommand("Select * from logtable where eid ='" + txtuname.Text + "'", cnn);
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            
            Session["eid"] = dr["eid"].ToString();
            Session["password"] = dr["password"].ToString();
            dr.Close();
            Response.Redirect("seekerhome.aspx");
        }
        else
        {
            txtuname.Text = "";
            txtpasswd.Text = "";
            lblmsg.Text = "Either E-Mail ID or Password is wrong..!";
        }

        cnn.Close();
    }



so can you help me following this code how can i display my last log in date?
I also confused in this logic also.
Posted

Hi,
It seems you are not storing log in date into your DataBase so the First thing is When user log in store the log in date in your dateBase and in the next time and you can show it to user by simple Query
Another thing you can use parametrize Query so you can Avoid SQL Injections
Best Regards
M.Mitwalli
 
Share this answer
 
Always use parameterised Query, so that SQL injection won't be done.

C#
protected void btnlogin_Click(object sender, EventArgs e)
    {
        string q = "select count(*) from logdate where eid = @Name and password = @Password";
        cmd = new SqlCommand(q, cnn);
        cmd.Parameters.Add("@Name",  txtuname.Text.Trim());
        cmd.Parameters.Add("@Password", txtpasswd.Text.Trim());        
        object obj;
        obj = cmd.ExecuteScalar();
        if (obj.ToString() == "1")
        {
            cmd = new SqlCommand("Select * from logtable where eid =@Name", cnn);
            cmd.Parameters.Add("@Name",  txtuname.Text.Trim());  
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            //Put the current login time in a session here and also insert into a table for accessing it next time when user logs in again.

            Session["eid"] = dr["eid"].ToString();
            Session["password"] = dr["password"].ToString();
            dr.Close();
            Response.Redirect("seekerhome.aspx");
        }
        else
        {
            txtuname.Text = "";
            txtpasswd.Text = "";
            lblmsg.Text = "Either E-Mail ID or Password is wrong..!";
        }

        cnn.Close();
    }


Thanks
Ashish
 
Share this answer
 
Comments
aarohi verma 23-Jul-12 5:54am    
thanks.i will try for date.then remark yr ans.
AshishChaudha 28-Aug-12 23:56pm    
Aarohi..Is my solution doesnt work for you?? You accepted my solution as your answer then undo..If you have any problem then tell us..and If your my solution is your answer then mark as answer so other can refer to the solution..
Thanks
You should read my article about security. Your code is vulnerable for sql-injections and you are storing passwords are clear text.

Beginners guide to a secure way of storing passwords[^]

To show last login date, just store the date now in your table when a user is logging in successfully.
 
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