Rajat
Rajat

Reputation: 3

Recording time/date from C# windows application form is inserted into access database?

I have developed one c# basic form, when i click submit the data is getting populated in database, But i also want that time & date also get populated in database once the user will click the form, so as to know when he submitted this form.

Can somebody Help.!!

Below is source code of whatever i have done till now.

namespace testing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }
    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }
    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }

    private void submit_Click(object sender, EventArgs e)
    {


           OleDbConnection cnon = new OleDbConnection();
       cnon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\visual_c\Database71.accdb";
       OleDbCommand command = new OleDbCommand();
       command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number,Emp_id) VALUES (@Asset_name, @Asset_number,@Emp_id)";
           command.Parameters.AddWithValue("@Assetname", textBox1.Text);
           command.Parameters.AddWithValue("@Assetnumber", textBox2.Text);
           command.Parameters.AddWithValue("@emp_id", textBox3.Text);
       cnon.Open();
       command.Connection = cnon;
       command.ExecuteNonQuery();
       cnon.Close();

    }        
}

}

Upvotes: 0

Views: 1143

Answers (2)

Fionnuala
Fionnuala

Reputation: 91366

Why not

   INSERT INTO electricity (Asset_name,Asset_number,Emp_id,timestamp) 
   VALUES (@Asset_name, @Asset_number,@Emp_id,now())

Or better, just set the default value of the timestamp field to Now() in the table, so there is no need to worry.

add timestamp

Upvotes: 1

mnieto
mnieto

Reputation: 3874

Your best is to declare a new field in electricity and set its default value to =now() You don't need to modify your code.

Upvotes: 1

Related Questions