I am using a gridview that is connected to a table adpater. On Insert of a new row I would like for a specific date column to be updated with the system date or the DateTime.Now. I have attempted to set the default value to DateTime.Now() and that did not work for me. Does anyone have a solution for this?
Thanks!
Jeff
![]() |
0 |
![]() |
You can use trigger in this case.
Thanks
Pankaj Gohel
Please Mark as Answer if you find the post useful.
![]() |
0 |
![]() |
convert that field to template field first, then set the default value to dateTime.now.
in dataset, for insert, use the default value in your query. if you use stored procedure, you don't need to add the date field to the input parameters.
Lynn
Please mark replies that have helped you as answers.
http://www.sis.pitt.edu/~lberan/collegeMathTutorPittsburgh/index.html
![]() |
0 |
![]() |
convert that field to template field first, then set the default value to dateTime.now.
in dataset, for insert, use the default value in your query. if you use stored procedure, you don't need to add the date field to the input parameters.
Lynn
Please mark replies that have helped you as answers.
http://www.sis.pitt.edu/~lberan/collegeMathTutorPittsburgh/index.html
![]() |
0 |
![]() |
lberan:
convert that field to template field first, then set the default value to dateTime.now.
in dataset, for insert, use the default value in your query. if you use stored procedure, you don't need to add the date field to the input parameters.
I am using a table adapter (dataset). I am unaware of how to complete this task, can you provide me with a sample?Thanks!
Jeff
![]() |
0 |
![]() |
please post you dataset query for insert and i'll modify it for you
Lynn
Please mark replies that have helped you as answers.
http://www.sis.pitt.edu/~lberan/collegeMathTutorPittsburgh/index.html
![]() |
0 |
![]() |
I am not sure which Insert statement you wanted.
From Dataset:
<InsertCommand><
DbCommand CommandType="Text" ModifiedByUser="false"><CommandText>INSERT INTO [dbo].[TaskList] ([TaskLeader], [Description], [Notes], [CreationDate], [DeadlineDate], [Completed]) VALUES (@TaskLeader, @Description, @Notes, @CreationDate, @DeadlineDate, @Completed)</CommandText><
Parameters><Parameter AllowDbNull="true" AutogeneratedName="" DataSourceName="" DbType="String" Direction="Input" ParameterName="@TaskLeader" Precision="0" ProviderType="NVarChar" Scale="0" Size="0" SourceColumn="TaskLeader" SourceColumnNullMapping="false" SourceVersion="Current" /><
Parameter AllowDbNull="true" AutogeneratedName="" DataSourceName="" DbType="String" Direction="Input" ParameterName="@Description" Precision="0" ProviderType="NVarChar" Scale="0" Size="0" SourceColumn="Description" SourceColumnNullMapping="false" SourceVersion="Current" /><Parameter AllowDbNull="true" AutogeneratedName="" DataSourceName="" DbType="String" Direction="Input" ParameterName="@Notes" Precision="0" ProviderType="NVarChar" Scale="0" Size="0" SourceColumn="Notes" SourceColumnNullMapping="false" SourceVersion="Current" /><
Parameter AllowDbNull="true" AutogeneratedName="" DataSourceName="" DbType="DateTime" Direction="Input" ParameterName="@CreationDate" Precision="0" ProviderType="DateTime" Scale="0" Size="0" SourceColumn="CreationDate" SourceColumnNullMapping="false" SourceVersion="Current" /><Parameter AllowDbNull="true" AutogeneratedName="" DataSourceName="" DbType="DateTime" Direction="Input" ParameterName="@DeadlineDate" Precision="0" ProviderType="DateTime" Scale="0" Size="0" SourceColumn="DeadlineDate" SourceColumnNullMapping="false" SourceVersion="Current" /><
Parameter AllowDbNull="true" AutogeneratedName="" DataSourceName="" DbType="Boolean" Direction="Input" ParameterName="@Completed" Precision="0" ProviderType="Bit" Scale="0" Size="0" SourceColumn="Completed" SourceColumnNullMapping="false" SourceVersion="Current" /></Parameters></
DbCommand></
InsertCommand>
From aspx page:
<
InsertParameters> <asp:Parameter Name="TaskLeader" Type="String" /> <asp:Parameter Name="Description" Type="String" /> <asp:Parameter Name="Notes" Type="String" /> <asp:Parameter Name="CreationDate" Type="DateTime" DefaultValue="DateTime.Now()" /> <asp:Parameter Name="DeadlineDate" Type="DateTime" /> <asp:Parameter Name="Completed" Type="Boolean" /> </InsertParameters>
Jeff
![]() |
0 |
![]() |
change this: <CommandText>INSERT INTO [dbo].[TaskList] ([TaskLeader], [Description], [Notes], [CreationDate], [DeadlineDate], [Completed]) VALUES (@TaskLeader, @Description, @Notes, @CreationDate, @DeadlineDate, @Completed)</CommandText>
to:
<CommandText>INSERT INTO [dbo].[TaskList] ([TaskLeader], [Description], [Notes], [CreationDate], [DeadlineDate], [Completed]) VALUES (@TaskLeader, @Description, @Notes, DateTime.Now(), @DeadlineDate, @Completed)</CommandText>2. delete this:
<Parameter AllowDbNull="true" AutogeneratedName="" DataSourceName="" DbType="DateTime" Direction="Input" ParameterName="@CreationDate" Precision="0" ProviderType="DateTime" Scale="0" Size="0" SourceColumn="CreationDate" SourceColumnNullMapping="false" SourceVersion="Current" />
3. delete this:
<asp:Parameter Name="CreationDate" Type="DateTime" DefaultValue="DateTime.Now()" />
Lynn
Please mark replies that have helped you as answers.
http://www.sis.pitt.edu/~lberan/collegeMathTutorPittsburgh/index.html
![]() |
0 |
![]() |
Thanks for the response. I am recieving the following error:
ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'Insert' that has parameters: TaskLeader, Description, Notes, DeadlineDate, Completed, CreationDate.
Any idea's?
Thanks again!
Jeff
![]() |
0 |
![]() |
sorry about that. DateTime.Now() is a asp.net function.
in a sql query, getdate() should be used.
Lynn
Please mark replies that have helped you as answers.
http://www.sis.pitt.edu/~lberan/collegeMathTutorPittsburgh/index.html
![]() |
0 |
![]() |
Did you read pankajgohel response? It's, in my opinion, the easiest and cleanest solution.
Edit: not only that, but if someone were to insert a record directly from the DB, you would still be populating the date.
C# <---> VB.Net Translator
![]() |
0 |
![]() |
MetalAsp.net is right. there are better solutions. besides trigger, you could set the default value for the field in the database.
Lynn
Please mark replies that have helped you as answers.
http://www.sis.pitt.edu/~lberan/collegeMathTutorPittsburgh/index.html
![]() |
0 |
![]() |
I would like to thank everyone for your responses.
Lynn: I was still recieving an error after I did what you suggested.
With that said, I decided to try a trigger on the insert of a new record for that particular table. The problem I am running into now is that the date is being inserted into a new record instead of the record that was created. My knowledge on trigger's is very limited. Does anyone have an idea why this is not working for me? I attempted to add a "where" clause, but it would not allow it.
Trigger:
SET
ANSI_NULLS ONGO
SET
QUOTED_IDENTIFIER ONGO
CREATE TRIGGER trig_Log_Insert_TaskList ON dbo.TaskListFor INSERT
AS
BEGIN
SET NOCOUNT ON; Insert into TaskList (CreationDate) Values (getdate())END
Thanks again for all your help!
Jeff
![]() |
0 |
![]() |
i honestly don't know how trigger works in this situation. However, nothing can be simpler than setting default value for creationDate. why don't you give it a try?
Lynn
Please mark replies that have helped you as answers.
http://www.sis.pitt.edu/~lberan/collegeMathTutorPittsburgh/index.html
![]() |
0 |
![]() |
Try this
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER trig_Log_Insert_TaskList ON dbo.TaskList
For INSERT
AS
BEGIN
SET NOCOUNT ON;
Update TaskList SET CreationDate = getdate() where ID In (Select ID FROM inserted)
END
Where ID is the primary key of Table TaskList
Thanks
Pankaj Gohel
Please Mark as Answer if you find the post useful.
![]() |
0 |
![]() |
Simple you can alter your table as
ALTER TABLE [TableName] WITH NOCHECK
ADD CONSTRAINT [Df_tblname_colName] DEFAULT DefaultValue FOR colNameIn your case
ALTER TABLE TaskList WITH NOCHECK
ADD CONSTRAINT Df_tblname_CreationDate DEFAULT 'getdate()' FOR CreationDateBy this when ever you add a column to your table and not specify this creationdate value its default value is assigned.
If you want the default value to be assigned dont pass the value and it would insert the default value.
Hopefully this must solve your problem. strictly no need for triggers in this case
Please Mark as Answer if this reply helps you :)
![]() |
0 |
![]() |
Simple you can alter your table as
ALTER TABLE [TableName] WITH NOCHECK ADD CONSTRAINT [Df_tblname_colName] DEFAULT DefaultValue FOR colName
n your case ALTER TABLE TaskList WITH NOCHECK ADD CONSTRAINT Df_tblname_CreationDate DEFAULT 'getdate()' FOR CreationDate
By this when ever you add a column to your table and not specify this creationdate value its default value is assigned. If you want the default value to be assigned dont pass the value and it would insert the default value.
Hopefully this must solve your problem. strictly no need for triggers in this case
Please Mark as Answer if this reply helps you :)
![]() |
0 |
![]() |