Click here to Skip to main content
15,895,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a web method that saves the current time and date to the database. I want to save the current time and date when a student has successfully registered. I have the date and time in a label form
private void timer1_Tick(object sender, EventArgs e)
        {
            lbTime.Text = DateTime.Now.ToLongTimeString();

        }

lbDate.Text = DateTime.Now.ToLongDateString();


Attendance record = new Attendance();
     record.StudentID = a.StudentID;
     record.RegistrationDate = lbDate.Text;
     record.RegistrationTime = lbTime.Text;


How do I save this onto the database. Datatype for Data in the DB is date and time as Time(7)
Posted

There is no need to handle the date and time in your code at all, leave it to the sql server,
1. There is no need to have separate date and time fields in the database table, just one datetime field will do.
2. Set the default value of this datetime field to the current date using getdate(). With that, whenever you insert a row (new student registration) into the table, the current date and time are automatically inserted into this field.
Read more: getdate[^]
 
Share this answer
 
v4
Save in one database column of type DateTime.

Use SQLParameter Class to create objects to store the paramaters for your INSERT or UPDATE statement. Use DateTime.Now to set the date and time into the proper parameter.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900