Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have Write a Program to get Recent Login date time of a User and have to display on th web page in the label. and getting error that

System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.String'.


The Store Procedure is as
SQL
CREATE PROC uspGetRecentLogin
@UserID int,@RLoginDate datetime OUT
AS
BEGIN
SET @RLoginDate= (SELECT TOP 1 LoginDate FROM TblLoginDetails WHERE UserId=@UserID ORDER BY LoginDate DESC)
END


The C# Code is
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class CandMaster : System.Web.UI.MasterPage
{
    public String AppUserName;
    int AppUserID;
    DBClass db1 = new DBClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AppUserName"] != null & Session["AppUserID"] != null)
        {

            AppUserName = Convert.ToString(Session["AppUserName"]);
            AppUserID = Convert.ToInt32(Session["AppUserID"]);
        }
        else {  }
        LblUserLogin.Text = AppUserName;
        
        GetRecentLoginTime();
    }



    private void GetRecentLoginTime()
    {
    try
    {
             DBClass db1 = new DBClass();
             db1.sqlcmd = new SqlCommand("uspGetRecentLogin");

            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                db1.sqlcmd.CommandType = CommandType.StoredProcedure;
                db1.sqlcmd.Parameters.AddWithValue("@UserID", AppUserID);
                db1.sqlcmd.Parameters.Add("@RLoginDate", SqlDbType.DateTime);
                db1.sqlcmd.Parameters["@RLoginDate"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Connection = db1.sqlcon;
                db1.sqlcon.Open();
                db1.sqlcmd.ExecuteScalar();
               LabelLastLogin.Text = (string) db1.sqlcmd.Parameters["@RLoginDate"].Value;
            }
        }
        catch (Exception ex) { Response.Write(ex);}
        finally { }
    
    
    
    
    }
}
Posted
Comments
[no name] 16-Apr-14 19:00pm    
Did you maybe try ToString()?
syed shanu 16-Apr-14 20:49pm    
Change ,@RLoginDate datetime OUT
to ,@RLoginDate varchar(20) OUT.In your code you displaying the output to lable only so need of datetime.and also in query chk for null value.
Muhammad Taqi Hassan Bukhari 17-Apr-14 1:35am    
That's Work fine. But i have now a problem, that i want to get recent login time, but my program is gives me the current login time. because its store procedure is called first.

CREATE PROC uspGetRecentLogin
@UserID int,@RLoginDate varchar(20) OUT
AS
BEGIN
SET @RLoginDate= (SELECT TOP 1 LoginDate FROM TblLoginDetails WHERE UserId=@UserID ORDER BY LoginDate DESC)
END

I have think a solution to simply get the 2nd last login date.
Now I want to know that how could i get the 2nd last recent login data against a User id.
j snooze 17-Apr-14 17:37pm    
You could use a subquery
SELECT TOP 1 LoginDate
FROM TblLoginDetails
WHERE UserId=@UserID
And LoginDate < (Select Max(LoginDate)FROM TblLoginDetails
WHERE UserId=@UserID)
ORDER BY LoginDate DESC
j snooze 17-Apr-14 17:39pm    
I would actually do max on both
SELECT Max(LoginDate)
FROM TblLoginDetails
WHERE UserId=@UserID
And LoginDate < (Select Max(LoginDate)FROM TblLoginDetails
WHERE UserId=@UserID)

Because if you have lots of records, using an order by will slow things down tremendously.

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