Hello!

How can I insert into DB date using DetailsView. I have variable:

DateTime now=DateTime.UtcNow;

I need to send it's value into the database automatically when an user inserts new record in my DetailsView. The code:

<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" 
        AutoGenerateRows="False" CellPadding="4" DataKeyNames="id" 
        DataSourceID="SqlDataSource3" ForeColor="#333333" GridLines="None" 
        Height="50px" Width="1000px">
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <Fields>
            <asp:TemplateField HeaderText="Header" SortExpression="Header">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Header") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Header") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Header") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Date" SortExpression="Date">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Date") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Body" SortExpression="Body">
                <EditItemTemplate>
                
                    <asp:TextBox ID="TextBox3" runat="server" Height="150px" 
                        Text='<%# Bind("Body") %>' TextMode="MultiLine" Width="574px"></asp:TextBox>
                    <br />
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Height="150px" 
                        Text='<%# Bind("Body") %>' TextMode="MultiLine" Width="574px"></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Body") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Prebody" SortExpression="Prebody">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Prebody") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Prebody") %>'></asp:TextBox>
                    &nbsp;300 signs only.
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Prebody") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ShowInsertButton="True" />
        </Fields>
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:DetailsView>

Thank you.

Dani AI

Generated

A few practical ways to make sure a timestamp is inserted server‑side (and why you saw a NullReferenceException when you tried to set the TextBox in Page_Load).

As suggested there are multiple approaches; the safest is to let the database set the value so clients cannot change it. Add a DEFAULT constraint on the Date column and stop sending a Date value from the app. Example (SQL Server) to set an automatic UTC timestamp:

ALTER TABLE YourTable
ADD CONSTRAINT DF_YourTable_Date DEFAULT (GETUTCDATE()) FOR [Date];

If you must set the value in the ASP.NET app (for example you need app logic or a different timezone), do it at insert time on the server rather than trying to mutate template controls in Page_Load. Handle the DetailsView insert event and inject the value into the insert values:

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
    e.Values["Date"] = DateTime.UtcNow; // or DateTime.Now if you need server local time
}

If you are using a SqlDataSource, you can also catch its Inserting event and set the command parameter value there (set the SqlParameter before the command runs).

Why the Page_Load NullReference happened: InsertTemplate controls are not instantiated during every Page_Load, so FindControl can return null if the DetailsView hasn’t created its Insert item. That’s why doing the assignment in ItemInserting (or SqlDataSource.Inserting) is more reliable. Final tips: remove the visible insert control for Date (or make it hidden/read‑only) so users can’t change it, and be consistent about UTC vs local time across app and DB.

Recommended Answers

All 8 Replies

>I need to send it's value into the database automatically when an user inserts new record in my DetailsView.

Assign a date value to the parameter.

>I need to send it's value into the database automatically when an user inserts new record in my DetailsView.

Assign a date value to the parameter.

Thanks for answer, but where can I assign the value of var.

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>

Can you show me how, please?

>but where can I assign the value of var

Two ways:
1. Parameter of datasource.
2. Using FindControl method you may assign date value to TextBox2.Text property.

>but where can I assign the value of var

Two ways:
1. Parameter of datasource.
2. Using FindControl method you may assign date value to TextBox2.Text property.

Well, in first case I want just date to be inserted in database explicitly (user can't change it).

Second case: date will be read from DB and user will have possibility to change it. (will show in textbox)

>Second case: date will be read from DB and user will have possibility to change it. (will show in textbox)

Not really that. Have a reference of TextBox and do assign a date.

TextBox tx=(TextBox)DetailsView1.FindControl("TextBox2");
  tx.Text=DateTime.Now.ToString()

>Second case: date will be read from DB and user will have possibility to change it. (will show in textbox)

Not really that. Have a reference of TextBox and do assign a date.

TextBox tx=(TextBox)DetailsView1.FindControl("TextBox2");
  tx.Text=DateTime.Now.ToString()

Hello Adatapost!

Did I right? I've placed the code you've provided me in Page_Load. Well now it says:

Object reference not set to an instance of an object.

What's now?

Perhaps there's some possibility to put

1. Hidden Field
or
2. Label

and to assign to him some value which can be inserted into DB?

>Perhaps there's some possibility to put

A column with default value constraints.

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.