0

I have a login page linked to a login db table with 3 columns namely ID, Pass and AccessTime. Whenever the login ID is successful, I need to update the respective record with the date and time of access.

UPDATE Login SET AccessTime = '"+DateTime.Now+"' WHERE ID = '" + id + "' ;"

And, retrieve the ID and the AccessTime in another page in a textbox.

I've used sessions for the retrieval process.

Now, how do I implement this on C# ASP.NET? I'm a newbie...

Ɖiamond ǤeezeƦ
  • 3,223
  • 3
  • 28
  • 40
gRao92
  • 91
  • 1
  • 8
  • start coding and share the challenge/error you facing. – kudlatiger Apr 07 '16 at 10:00
  • You should keep information like ID and accessTime in sessions only if you are going to require it across all or most pages of your website. Unnecessary information in session state will make your pages bulkier and hence sluggish page load time will happen eventually. If you are going to need it only once on the next page where a text box control is present then simply query the database to get the access time based on ID. Also if access time is available on first page then simply pass it in query string in the URL to pass it to the next page having that text box. – RBT Apr 07 '16 at 10:45

2 Answers2

1

First, learn the basics of ASP.NET http://www.asp.net/web-pages/overview/getting-started

Second, Create a procedure

      ALTER PROCEDURE SP_TRACK_USERLOG
      @id varchar(100),
      @password varchar(100)
      AS
      BEGIN
          SET NOCOUNT ON;
          INSERT INTO LOGIN(ID,Pass ,AccessTime)  VALUES (@id,@password ,GETDATE())

       END

Third, In SERVER side call the SP as follow

     SqlCommand cmd = new SqlCommand("SP_TRACK_USERLOG", con);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.AddWithValue("@id", Session["id"].ToString());
     cmd.Parameters.AddWithValue("@password", Session["password"].ToString());
     cmd.ExecuteNonQuery();

Also, learn about TimeZone Daylight saving time and time zone best practices . This will help in long run.

Learn about Connection String in asp.net How to create a connection string in asp.net c#

Community
  • 1
  • 1
kudlatiger
  • 3,028
  • 8
  • 48
  • 98
0

You should use using:

using System.Data;
using System.Data.SqlClient;

Then create your connection, open your connection, write your SQL command, and SQL adapter

    static void Main(string[] args)
    {
        SqlConnection conn = new SqlConnection(@"Data Source = .\SQLEXPRESS; Initial Catalog=AdventureWorks2014; Integrated Security=SSPI");
        conn.Open();

In the sql command you should use sql parameters

        //somthing like thes
        SqlParameter param = new SqlParameter();
        param.ParameterName = "@param";
        param.Value = TheValue;

Then you could use them in the sql command

        SqlCommand cmd = new SqlCommand("UPDATE Login SET AccessTime = '"+DateTime.Now+"' WHERE ID = '" + id + "' ;"", conn);
      //add parameters
       cmd.Parameters.Add(param);
       cmd.ExecuteScalar();
       conn.Close();
       conn.Dispose();
    }
kudlatiger
  • 3,028
  • 8
  • 48
  • 98