Click here to Skip to main content
15,888,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friendss,

I have a Webapplication project, in that i used to display users login and logout history.

On my webform I have a Gridview which is used to display Users Login History.

I used a Method to display the data in gridview and binded it....its displaying fine.
I didnt use Gridview boundfields to display the data.

In this Gridview I used fields as userid,username, login_time, logout_time and also i need to display the Duration time from login to logout as 2 hours something like this.

the time between login and logout is displaying...but it doesnt show column header in Gridview as duration.

I took Duration Field in Database....but unable to display.

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["infosystemDBConnectionString"].ToString();

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

    }
    protected void BtnGo_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);
        SqlCommand cmd = new SqlCommand("Select LogId,UserName,Login_Time,Logout_Time,convert(nvarchar,datediff(mi,Login_Time,Logout_time))) + ' Mins ' 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 solve my problem and give me a perfect solution. i need header as duration.

Thanks in Advance.

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 7-Jul-12 21:12pm
v2

1 solution

Two things:
1) Assuming that
SQL
convert(nvarchar,datediff(mi,Login_Time,Logout_time))) + ' Mins '
is the column you want as the duration, just add "AS Duration" to the end of it:
SQL
Select LogId,UserName,Login_Time,Logout_Time,convert(nvarchar,datediff(mi,Login_Time,Logout_time))) + ' Mins ' from UserLogs
Becomes
SQL
Select LogId,UserName,Login_Time,Logout_Time,convert(nvarchar,datediff(mi,Login_Time,Logout_time))) + ' Mins ' AS Duration from UserLogs


2) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Building your command that way allows me to connect to your site from anywhere in the world and delete your entire database in about twenty simple keystrokes.
 
Share this answer
 
Comments
Ubaid ur Rahman IT 8-Jul-12 4:12am    
Thank you Soooooooooooooo much boss, its Working.
OriginalGriff 8-Jul-12 4:13am    
You're welcome!

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