Showing posts with label users. Show all posts
Showing posts with label users. Show all posts

Thursday, March 22, 2012

datetimestamp into db

as you can see i am trying to figure out how to get the users IP address and a datetimestamp into my db. can anyone assist. i tried to comment out the datetimestamp but the iprequest does not work either.

The site i am workingon is papertrailinvites.com the newsleter on the left hand side there. i am trying to collect the email address, ip address and a datetimestamp. inserting the email address works now i just need to get the ipaddress and the datetimestamp in the db

code is below

Thanks

using (SqlConnection conn =newSqlConnection(ConfigurationManager.ConnectionStrings["cardsConnectionString1"].ConnectionString)) {

String newemail = Newsletter_Email.Text;

SqlCommand cmd =newSqlCommand("INSERT INTO Email (EmailAddress, IPAddress, DateTimeStamp) Values (@.EmailAddress, @.IPAddress, @.DateTimeStamp)", conn);

cmd.CommandType =CommandType.Text;

cmd.Parameters.AddWithValue("@.EmailAddRess", newemail);

cmd.Parameters.AddWithValue("@.IPAddress",HttpContext.Current.Request.UserHostAddress);

//cmd.Parameters.AddWithValue("@.DateTimeStamp",

conn.Open();

cmd.ExecuteNonQuery();

Hi,

You could try changing the commented line to:

cmd.Parameters.AddWithValue("@.DateTimeStamp", DateTime.Now.ToString());

However, I normally set a default value in the table itself to getdate() which means you don't need to worry about this field at all. Alternatively, you could add to your SQL:

DECLARE @.DateCreatedAS DateTimeSET @.DateCreated =GetDate()
This is the same as setting the default value, and again means you don't need to pass it as a parameter.
Hope this helps,
Paul

Wednesday, March 21, 2012

datetime problem

my asp .net application is coming along nicely. however, i would like to record the users last login time. i have a datetime field, and when i insert or update using Now() as the data for the field, i simply get 1/1/1900 12:00:00 in the last_login field. Same thing using Today(). any suggestions? am i using the wrong data type? i need to be able to do date comparisons as i plan on connecting my site with a forum, and this would be the easiest way to determine if there were new posts from the user's perspective.

TIA

Use GetDate() in your SQL to input the current time.|||

Unless your business object has a say in what the date/time stamp value is, there is little reason to send it over the network both ways. Might as well have the database provide the value and hand it back to you.

|||

thanks mikesdotnetting! worked like a charm!

|||

Don't forget that getdate(0 and Now() work on different servers which can be in different time zones :)

Monday, March 19, 2012

DateTime Now Problem

Hey, basically i have a database of users and on a certain page load i want to register when they logged onto that page, so i created this snippet of code :

SqlCommand UpdateMyTime = new SqlCommand("UPDATE Users SET LastUpdated = '" + DateTime.Now + "' WHERE UserID = '" + CurrentUserID + "'", Connection);
UpdateMyTime.ExecuteNonQuery();


but that give me the error :

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.

So I changed it to :

SqlCommand UpdateMyTime = new SqlCommand("UPDATE Users SET LastUpdated = '" + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "' WHERE UserID = '" + CurrentUserID + "'", Connection);
UpdateMyTime.ExecuteNonQuery();

as when I looked at previous times in my database there format was "27/08/2007 15:25:45" for example. but that gives me the exact same error can anyone tell me why this doesnt work? / how to get it to work? My datatype for this feild is "datetime". Thanks John

SqlCommand UpdateMyTime = new SqlCommand("UPDATE Users SET LastUpdated= '" + DateTime.Now.ToString() + "' WHERE UserID = '" + CurrentUserID + "'",Connection);
UpdateMyTime.ExecuteNonQuery();

OR , best way is

SqlCommand UpdateMyTime = new SqlCommand("UPDATE Users SETLastUpdated= GetDate() WHERE UserID = '" + CurrentUserID + "'",Connection);
UpdateMyTime.ExecuteNonQuery();

|||

Awesome this :

SqlCommand UpdateMyTime = new SqlCommand("UPDATE Users SETLastUpdated= GetDate() WHERE UserID = '" + CurrentUserID + "'",Connection);
UpdateMyTime.ExecuteNonQuery();

way did work the best, for anyone else who is having this problem, thanks loads! John