Get date and write it to the DB
I have another post I think keeps getting overlooked... I was wondering if someone would look at my code Here and tell me how I could add a part that would write the current time/date to the database when the rest of the information is written!
Thanks!!
Anjari
Re: Get date and write it to the DB
You need to add another field to the database to store the date and then update your insert statement to include this field.
You then have two choices either setup a new Parameter which has the value System.DateTime.Now() as Magiaus suggested on the other post or just use GETDATE() in the SQL statement which will make SQL Server enter the current date not needing any addition command parameters.
Code:
strInsert = "insert t_manufacturer (manufacturer_name, manufacturer_address1, manufacturer_address2, manufacturer_city, manufacturer_state, manufacturer_zip, manufacturer_country, manufacturer_account, manufacturer_url, manufacturer_primary_contact, manufacturer_primary_telephone, manufacturer_primary_telephone_ext, manufacturer_primary_email, date_added ) values ( @txtManufacturerName, @txtAddress1, @txtAddress2, @txtCity, @txtState, @txtZip, @txtCountry, @txtAccount, @txtManufacturerURL, @txtPrimaryContact, @txtPrimaryContactPhone, @txtPrimaryContactExt, @txtPrimaryContactEmail, @dateAdded )"
cmdInsert = New SqlCommand( strInsert, myConnection )
cmdInsert.Parameters.Add("@txtManufacturerName", txtManufacturerName.Text )
cmdInsert.Parameters.Add("@txtAddress1", txtAddress1.Text )
cmdInsert.Parameters.Add("@txtAddress2", txtAddress2.Text )
cmdInsert.Parameters.Add("@txtCity", txtCity.Text )
cmdInsert.Parameters.Add("@txtState", txtState.Text )
cmdInsert.Parameters.Add("@txtZip", txtZip.Text )
cmdInsert.Parameters.Add("@txtCountry", txtCountry.Text )
cmdInsert.Parameters.Add("@txtAccount", txtAccount.Text )
cmdInsert.Parameters.Add("@txtManufacturerURL", txtManufacturerURL.Text )
cmdInsert.Parameters.Add("@txtPrimaryContact", txtPrimaryContact.Text )
cmdInsert.Parameters.Add("@txtPrimaryContactPhone", txtPrimaryContactPhone.Text )
cmdInsert.Parameters.Add("@txtPrimaryContactExt", txtPrimaryContactExt.Text )
cmdInsert.Parameters.Add("@txtPrimaryContactEmail", txtPrimaryContactEmail.Text )
cmdInsert.Parameters.Add("@DateAdded", System.DateTime.Now())
OR
Code:
strInsert = "insert t_manufacturer (manufacturer_name, manufacturer_address1, manufacturer_address2, manufacturer_city, manufacturer_state, manufacturer_zip, manufacturer_country, manufacturer_account, manufacturer_url, manufacturer_primary_contact, manufacturer_primary_telephone, manufacturer_primary_telephone_ext, manufacturer_primary_email, date_added ) values ( @txtManufacturerName, @txtAddress1, @txtAddress2, @txtCity, @txtState, @txtZip, @txtCountry, @txtAccount, @txtManufacturerURL, @txtPrimaryContact, @txtPrimaryContactPhone, @txtPrimaryContactExt, @txtPrimaryContactEmail, GETDATE() )"
cmdInsert = New SqlCommand( strInsert, myConnection )
cmdInsert.Parameters.Add("@txtManufacturerName", txtManufacturerName.Text )
cmdInsert.Parameters.Add("@txtAddress1", txtAddress1.Text )
cmdInsert.Parameters.Add("@txtAddress2", txtAddress2.Text )
cmdInsert.Parameters.Add("@txtCity", txtCity.Text )
cmdInsert.Parameters.Add("@txtState", txtState.Text )
cmdInsert.Parameters.Add("@txtZip", txtZip.Text )
cmdInsert.Parameters.Add("@txtCountry", txtCountry.Text )
cmdInsert.Parameters.Add("@txtAccount", txtAccount.Text )
cmdInsert.Parameters.Add("@txtManufacturerURL", txtManufacturerURL.Text )
cmdInsert.Parameters.Add("@txtPrimaryContact", txtPrimaryContact.Text )
cmdInsert.Parameters.Add("@txtPrimaryContactPhone", txtPrimaryContactPhone.Text )
cmdInsert.Parameters.Add("@txtPrimaryContactExt", txtPrimaryContactExt.Text )
cmdInsert.Parameters.Add("@txtPrimaryContactEmail", txtPrimaryContactEmail.Text )
I personally prefer the second method.
HTH
DJ