Link to home
Start Free TrialLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

Save the date in the table and retrieve it ASP.Net Vb.Net

Hi Experts,

I have a textbox which accepts a date  for example "06/21/2017".
I use "prmDateClosed = New SqlParameter("@DateClosed", txtDateClosed.Text.Trim)" to save into the table. The data type of the field in the table is "Datetime". It saves in the table as "2017-06-26 00:00:00.000". When I retrieve it back in a gridview it displayes "6/21/2017 12:00:00 AM ". I just want to save in the table as "06/21/2017".  and retrieve as "06/21/2017".

Thanks in advance.
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Hi,

<asp:BoundField DataField="Your_DateColumnName" DataFormatString="{0:d}" HtmlRncode="false" />

Open in new window

Just to explain. It's important to understand, that you do not save a date in some specific format. You apply formatting when you display the date. I.e., a DateTime doesn't have any implicit format, it just date and time components. String representations of a DateTime may have a format. Same is true for SQL server  - it saves date and time components, and how you view them is a different story.
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

ASKER

Hi Mr. Lokesh,

This is my code.

  <asp:TemplateField HeaderText="Date" >
                                <HeaderStyle Width="100px" HorizontalAlign="Left" />
                                <ItemStyle Width="100px" HorizontalAlign="Left" />
                                <ItemTemplate>
                                    <asp:Label ID="lblDateClosedID" runat="server" Text='<%#Eval("ID")%>' ItemStyle-HorizontalAlign="Center"  visible="false" ></asp:Label>
                                    <asp:linkbutton ID="lblDateClosed" runat="server" Text='<%#Eval("DateClosed")%>' ItemStyle-HorizontalAlign="Center" CommandName ="EditDate" DataFormatString="{0:d}" HtmlRncode="false" ></asp:linkbutton>
                                </ItemTemplate>
                            </asp:TemplateField>

But it did not make any difference.
or use this in your query

convert(datetime, myDateTimeColumn, 101) as myDateTimeColumn 

Open in new window


so you get data from sql as "mm/dd/yyyy"
or go to your db and change datatype to date in that table if you really do not need any time part at all!
ASKER CERTIFIED SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. It Worked!!