toly P
toly P

Reputation: 62

passing a Datetime from SQL to an asp textbox but getting different result

I am passing a DateTime value from SQL Table to a Textbox in ASP.Net using

<asp:TextBox ID="dateTxt" runat="server" Text='<%# Eval("date").ToString()%>' class="form-control"></asp:TextBox>

The Value in SQL is 2023-02-08 16:01:49.153

But shows in the textbox as 2/8/2023 09:07:13 PM

I want it to show as the exact SQL value.

This is how I get the value from the Database

SqlCommand cmd = new SqlCommand("SELECT *   FROM   ThisTable where stage = 'pending'", con)
                    {
                        CommandType = CommandType.Text
                    };
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();

Upvotes: 1

Views: 96

Answers (3)

Praveen Mishra
Praveen Mishra

Reputation: 101

I have used this it works for me

Text='<%# Eval("date").ToString("yyyy-MM-dd hh:mm:ss")%'

Upvotes: 1

toly P
toly P

Reputation: 62

I needed to add

Text='<%# Eval("date", "{0:yyyy-MM-dd HH:mm:ss.fff}") %>'

and that worked for me.

Upvotes: 0

Md. Suman Kabir
Md. Suman Kabir

Reputation: 5453

Try this :

Eval("date").ToString("yyyy-MM-dd hh:mm:ss")

Upvotes: 0

Related Questions