Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Display Login history according to System date.
===============================================

Hi FRNDS !!

I have Login history of the User, UserLogin details and UserLogout Details.

In GRIDVIEW it shows all the Records. but the REcords are not displaying correctly.

I need the last Few minutes login in the First row and todays users login history in the first page of GRIDVIEW.

Note : according to Date Wise it should display in GRIDVIEW.

This is my code :
==================

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Users_UserLoginHistory : System.Web.UI.Page
{
    string conString = ConfigurationManager.ConnectionStrings["KKIA_RUHConnectionString"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridData();
        }
    }


    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(conString);


        SqlCommand cmd = new SqlCommand("Select LogId,UserName,Login_Time,Logout_Time from UserLogs where UserName='" + TxtSearch.Text + "' order by Login_Time desc", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        Grid1.DataSource = dt;
        Grid1.DataBind();
    }


    private void GridData()
    {
        SqlConnection con = new SqlConnection(conString);

        SqlCommand cmd = new SqlCommand("Select LogId,UserName,Login_Time,Logout_Time from UserLogs order by Login_Time desc", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        Grid1.DataSource = dt;
        Grid1.DataBind();

    }
}


Please help me,

Thanks.
Posted
Updated 24-Jun-12 22:32pm
v2
Comments
DamithSL 25-Jun-12 4:32am    
you can use pre tag when you put code sample inside the question.
<pre lang="c#">
Put your code here.......
</pre>
DamithSL 25-Jun-12 4:36am    
Don't use inline parameters. use Parameterized-Queries and also better to have using block when you working with disposable objects like SqlConnection, SqlCommand etc
Sandesh M Patil 25-Jun-12 4:39am    
All queries are correct. Whats the problem you are facing?
Vani Kulkarni 25-Jun-12 4:40am    
Your query seems fine. How is the data displayed? Can u post some sample data?
DamithSL 25-Jun-12 4:43am    
What is the data type you save Login_Time?

1 solution

Hi,

There are certain things to keep in mind here:
1) when you store login time you will pass dateTime.Now, that machine datetime, should be proper,
2) run the query in the DB and check, whether sorting happening properly with the values.
3) if you are using Date function in SQL Server then, that should be in sync with windows time, sometime ppl. don't set properly in the system or in the server, resulting the date, get stored in different formats and user end up in this kind of problems
 
Share this answer
 

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