1

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();

3 Answers 3

1

I have used this it works for me

Text='<%# Eval("date").ToString("yyyy-MM-dd hh:mm:ss")%'
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

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

1 Comment

I tried that but I needed the milliseconds and when I added the fff for milliseconds it would just put "000". If figured out how to do it though. Thank you for trying. I posted the answer.
0

I needed to add

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

and that worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.