O
O
Oleg Seledets2020-02-20 13:14:19
.NET
Oleg Seledets, 2020-02-20 13:14:19

How to fix The conversion of a nvarchar data type to a datetime data type?

Hello.
There are 2 GridViews, when a row is selected in the first, the related data should open in the second.
But when the "select" item is clicked, an exception is thrown:
"Exception details: System.Data.SqlClient.SqlException: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value."

private void LinesCount()
        {
            GridView2.DataBind();
       
            int count = GridView2.Rows.Count;
            Label1.Text = "Всего строк - " + count.ToString();
            if (count == 0)
                Button13.Visible = true;
            else
                Button13.Visible = false;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateChecking();
            LinesCount();
        }

Screenshots

5e4e5a77aba88285073735.jpeg5e4e5a7f96b2d089213081.jpeg


The error points to the string GridView2.DataBind();
The date in the database is specified in the required format, when you try to change the type from date to nvarchar, the error goes away, but the displayed data changes the display format.
Literally a month ago everything started up normally, did it again, still the same error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Seledets, 2020-02-22
@oleja1ee7

The answer turned out to be very simple, I'm sorry)
because. method function DateChecking received the data of the present time. then it was necessary to transform:

DateField.Value = DateTime.Now.ToShortDateString();

in
const string dateTimePattern = "yyyy.MM.dd";
            DateField.Value = DateTime.Now.ToString(dateTimePattern);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question