I have made a registration form using C# and the record will be saved in sql. I want to also add a record of the registration date automatically. How to do this?

I'm confused, are you wanting to record the date of the registration within the information you're recording about the registration? Or in a separate record/table?

If you simply want the current date/time within the same record it's as simple as adding a date/time column in your table and populating it either from the code-behind side with DateTime.Now or on the SQL side with (I think) dateTime.Now() (been a while since I used the commands directly within SQL).

commented: thanks +1

You can open your table in Desing mode,add the column of registration date with datatype of datetime then go to column properties and in "default value or binding" properties add "getDate()" without qout as value. Now save the table, From now on you do not need to do anything, whenever a new row is added this column value will be generated automatically... hope this helps...:)

commented: That's what I was getting at :) +1
cmd.Parameters.Add("@Date_Registered", SqlDbType.DateTime).Value = DateTime.Now;

why to increase code..

if you are using the current date and time

let the sql server help you..

mono_jit23 has given the best deal.. :)

your way okey but the thing is you can control

the TV via Remote..why do you need to go to TV and change the Channel Manually.. :)

commented: good example +1

I tried this but Insert Error: Column name or number of supplied values does not match table definition.

SQL

Date_Registered DATETIME DEFAULT GETDATE()

Ok, Umm... If you have SQL Server Management Tools available for you for the DB you're working on do this:

  1. choose the Date_Registered column within your table
  2. Column Properties > General > Default Value or Binding
  3. Enter "getDate()" (without quotes)

It may give you some grief over the fact that prior fields may or may not be already filled in but it will achieve what was suggested by mono.

Alternately, as this is now several days old and it's still not doing what you wanted it to do why not just give up on that if you can't figure it out and instead have the date generated on the application side and inserted at the time of the record being sent like I originally said :)

Either works, mono's method works behind the scenes and without fuss but you seem to be having difficulty implementing it.

See, this is what happens when I buy a new game and take a few days off DaniWeb :twisted:

commented: nice comeback.. xP +1

I even tried inserting a record in the table itself..

Here is an example:

CREATE Table tbl (Name nvarchar(50), Date_Registered DATETIME DEFAULT GETDATE())
INSERT INTO tbl VALUES ('mark')

I believe I don't need to put a second value because date_registered will automatically add a datetime.

Well if you set up the table to have the columnn auto-populate then you are correct, you would not need to add a value when submiting a row from your program.

It's only if you do NOT have the column auto-populating dateTime that you would still need to insert a value at the time that the new row is entered.

Even though I entered default getdate(), the value of default binding under general is empty. I tried putting getdate() but I can't enter a value inside it.

I choose modify instead properties and still with getdate(), it doesn't work.

Insert Error: Column name or number of supplied values does not match table definition.

It's working now if I include the columns names i.e

INSERT INTO tbl (name) values ('mark')

nice...!

Hi

Its nothing but it takes just current date and time for registration.you will refer the following code to solve the issue of registration date.

syntax:

CurrentDate.Text = DateTime.Now.ToString()

objCustomer.currentDate = txtDate.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.