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?
 
Luke101Connect With a Mentor Commented:
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
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
All Courses

From novice to tech pro — start learning today.