Return number of days.

Need the number of days since online.

public static int GetLastOnline(int userID)
        {
            // create data object and params
            SqlDataReader dataReader = null;
            DateTime dt = 0;
            int iDays;

            // create params for stored procedure call
            Database data = new Database();

            SqlParameter[] prams =
                  {                    
                        data.MakeInParam("@iUserID" ,SqlDbType.Int, 4, userID)                        
                  };

            // run the stored procedure
            data.RunProc("prcUserGetLastOnline", prams, out dataReader);
            while (dataReader.Read())
                dt = dataReader.GetInt32(13);
            dataReader.Close();

            // Calculate number of days DateTime.Now - dt
            return iDays;
        }
mathieu_cuprykAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cmcawoodCommented:
Firstly, you need to retrieve a date from your database, use GetDateTime instead of GetInt32.  The following code should do the calc for your then
TimeSpan diff = DateTime.Now - dt;
iDays = diff.Days;

Open in new window

0
Luke101Commented:
hope this helps
DateTime start = new DateTime(2003, 12, 12);
DateTime end = DateTime.Now;
 
TimeSpan t = end.Subtract(start);
Response.Write(t.Days);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mathieu_cuprykAuthor Commented:
Error      2      Use of unassigned local variable 'dt'      D:\Inetpub\wwwroot\omegalove\App_Code\UserDB.cs      188      44      D:\...\omegalove\
public int GetLastOnline(int userID)
        {
            // create data object and params
            SqlDataReader dataReader = null;
            DateTime dt;
            int iDays;

            // create params for stored procedure call
            Database data = new Database();

            SqlParameter[] prams =
                  {                    
                        data.MakeInParam("@iUserID" ,SqlDbType.Int, 4, userID)                        
                  };

            // run the stored procedure
            data.RunProc("prcUserGetLastOnline", prams, out dataReader);
            while (dataReader.Read())
               dt = dataReader.GetDateTime(13);
            dataReader.Close();

            TimeSpan diff = DateTime.Now - dt.Date;
            iDays = diff.Days;
            return iDays;
        }
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

cmcawoodCommented:
You need to initialise dt before you use it.
0
mathieu_cuprykAuthor Commented:
to what?
0
gnoonCommented:
If you're using .NET 2.0 or later you can assign it to Null. For prior version, use another value to represent Null instead, DateTime.MaxValue for example.

# 2.0+
public int GetLastOnline(int userID)
{
    ...
    DateTime? dt = null;
    ...
    if(dt != null)
    {
        return (DateTime.Now - dt.Date).Days;
    }
    return -1; // represent nothing returned from database
}

# Later
public int GetLastOnline(int userID)
{
    ...
    DateTime dt = DateTime.MaxValue;
    ...
    if(dt != DateTime.MaxValue)
    {
        return (DateTime.Now - dt.Date).Days;
    }
    return -1; // represent nothing returned from database
}
0
gnoonCommented:
># Later

it should be #1.x ;-)
0
mathieu_cuprykAuthor Commented:
I am confused. Can someone help me out.
0
Luke101Commented:
Instead of
DateTime dt = 0;

Try:
DateTime dt = new DateTime();
0
Luke101Commented:
Also,
If you are saving a date in your database make sure you assign dt that value then perform date arithmetic.
0
gnoonCommented:
>Error  2  Use of unassigned local variable 'dt'  D:\Inetpub\wwwroot\omegalove\App_Code\UserDB.cs      188      44      D:\...\omegalove\
The compiler detects that you're trying to use a Property of dt (dt.Date), which may be not initiated because you declare it like this

DateTime dt;

then you assign its value in a while loop, which may *not* happen depends on data read from database

while (dataReader.Read())
    dt = dataReader.GetDateTime(13);

To fix this, you need to make sure it's initiaited before use its property/method,  as Expert's comments above.
The problem of DateTime initiation is it cannot be Null in .NET version prior to 2.0. You need to assign it a DateTime value, which should not exist in database. So, after the while loop you will know that you got it from database or not. As my previous comment above, I recommend DateTime.MaxValue.
0
mathieu_cuprykAuthor Commented:
Conversion failed when converting the nvarchar value 'macupryk' to data type int
at the while.
 public int GetLastOnline(int userID)
        {
            // create data object and params
            SqlDataReader dataReader = null;
            DateTime dt = new DateTime();
            int iDays;

            // create params for stored procedure call
            Database data = new Database();

            SqlParameter[] prams =
            {                    
                data.MakeInParam("@iUserID" ,SqlDbType.Int, 4, userID)                        
            };

            // run the stored procedure
            data.RunProc("prcUserGetLastOnline", prams, out dataReader);
            while (dataReader.Read())
               dt = dataReader.GetDateTime(15);
            dataReader.Close();

            TimeSpan diff = DateTime.Now - dt;
            iDays = diff.Days;
            return iDays;
        }
0
gnoonCommented:
The conversion failed somewhere within the "prcUserGetLastOnline" procedure. Could you paste it here? So we will help you to fix it or find another solution.
0
gnoonCommented:
For quick answer, it's not possible to convert value 'macupryk' to int without transformation algorithm.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.