I have Database name LoginUser with corresponding fields name such as User, Login and Logout. Im done in inserting data once the user Log to the system. Heres the sample layout once the user tried to log.

User................Login............................Logout
Abigail.........12/29/2010 3:08:42 PM
James..........12/29/2010 3:09:45 PM
Abigail.........12/29/2010 3:30:45 PM

PROBLEM:

1) I want to update the user with the same username BUT HOW?
2) Plus how to update the logout field when the user try to exit the program?

Heres my code for now:

private void btnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection Connect = new SqlConnection();

            Connect.ConnectionString = @"Data Source=BURBANK\SQLEXPRESS;" + "Initial Catalog= ERPdb;" + "Persist Security Info=True;" + "User ID=antonette;" + "Password=ilovemis";
            Connect.Open();

            SqlCommand cmd = new SqlCommand("SELECT ISNULL(username, '') AS username, employeeID, ISNULL(password, '') AS password FROM employee WHERE username='" + txtUserName.Text + "' and password='" + txtPassword.Text + "'", Connect);

            string userText = txtUserName.Text;
            string passText = txtPassword.Text;
            string fisrt = txtempID.Text;

            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                frmMain main = new frmMain();
                txtempID.Text = Convert.ToString(dr[1]);
                main.Show();
                dr.Close();
                SqlCommand cmdLogin = new SqlCommand("INSERT INTO UserLoginLogs(EmployeeID,LoginDate)VALUES ('" + txtempID.Text + "',getDate())", Connect);
                SqlDataReader dr1 = cmdLogin.ExecuteReader();
            }
            else
                MessageBox.Show("Invalid UserName or Password.");
            {
                dr.Close();
            }

            try
            {
            }
            catch (SqlException ex)
            {
                MessageBox.Show("There is an Error" + ex);
            }
            finally
            {
                Connect.Close();
            }
        }

Hope someone can help me with this. Thanks in advance.

Just write a update statement in the Form close or Logout button to update the current user's information with the current time.

Create labels for the current user. And assign their values from your variables.
in the logout button or form close just run the following sql statement.

SqlCommand cmd = new SqlCommand("Update employee SET logout = "+Datetime.Now+" WHERE Username = "+usernamelbl.text+"")

code not tested.

Nothing has change on my database. Any other option?

Create labels for the current user. And assign their values from your variables.
in the logout button or form close just run the following sql statement.

SqlCommand cmd = new SqlCommand("Update employee SET logout = "+Datetime.Now+" WHERE Username = "+usernamelbl.text+"")

code not tested.

Non-numeric data must be enclosed within the single quotes.

SqlCommand cmd = new SqlCommand("Update employee SET logout ='"+Datetime.Now+"' WHERE Username = '"+usernamelbl.text+"'")

or use Parameters,

SqlCommand cmd = new SqlCommand("Update employee SET logout =@logout WHERE Username =@username")
cmd.Parameters.AddWithValue("@logout",DateTime.Now);
cmd.Parameters.AddWithValue("@username",usernamelbl.Text);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.