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

No comments:

Post a Comment