Q:

Saving login time during user login with C# window form application

Now I am trying to get the user log with C# window form Desktop application.
I tried 
    private void btn_Login_Click(object sender, EventArgs e)
    {
        string username = txt_Username.Text;
        string password = txt_Password.Text;

        if (username=="" && password=="")
        {
            MessageBox.Show("Please enter Username and Password");
        }
        else
        {

            Function.sqlfunction.connection.Open();
            string query = "adminlogin";
            SqlCommand cmd = new SqlCommand(query, Function.sqlfunction.connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@pass_word", password);
            int usercount = (Int32)cmd.ExecuteScalar();
            if (usercount == 1)  
            {
                string loginsert = "INSERT INTO tbl_loguser (username, loginTime) VALUES (@username,@time)";
                SqlCommand logcmd = new SqlCommand(loginsert, Function.sqlfunction.connection);
                logcmd.CommandType = CommandType.Text;
                logcmd.Parameters.AddWithValue("@username", username);
                logcmd.Parameters.AddWithValue("@time", DateTime.Now.ToString());
                Form next = new frm_Discount();
                next.Show();
                this.Hide();                    

            }
            else
            {

                MessageBox.Show("LoginId or Password Is wrong"); //if user Name is not available in database

            }
            Function.sqlfunction.connection.Close();

        }
    }  

I can enter into  my application, no data was save to tbl_logusers
how should I do next.Please help me with this .

A:

As far as I see, you never execute your command. You need;
logcmd.ExecuteNonQuery();

after you add parameter values.
Also use using statement to dispose your SqlConnection and SqlCommand instead of calling .Close() method manually.
And don't use AddWithValue anymore. It may generate unexpected results. Use .Add() method or it's overloads.
using(SqlConnection con = new SqlConnection(conString))
using(SqlCommand cmd = con.CreateCommand())
{
   // Set your CommandText propert of your query.
   // Add your parameter values with SqlParameterCollection.Add() method
   // Open your connection.
   // Execute your query.
}

I strongly suspect you try to save your DateTime value as a character in your time column. Don't do that! Pass your DateTime.Now value (DateTime.UtcNow might be preferable of course) directly to your datetime or related typed column.
Read: Bad habits to kick : choosing the wrong data type

