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.

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.