Getting error while passing ajax control date (dd/MM/yyyy) as parameter in ASP.NET

BeUnique 2,112 Reputation points
2024-06-20T13:59:43.9666667+00:00

I am using AJAX Calendar control inside the gridview control.

I have set the date format as "dd/MM/yyyy" and my SQL column as "DOJ" field data type is nvarchar(10).

End user want always the format like "dd/MM/yyyy".

I am trying to save ajax calendar text box value, such as "25/01/2024" and passing the value as parameter.

But, i am getting error as below.

User's image

How to handle the date format while sending and receiving the same format...?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,506 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,007 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 28,536 Reputation points
    2024-06-20T16:09:03.48+00:00

    and my SQL column as "DOJ" field data type is nvarchar(10).

    You should reconsider the design. Date fields should be DateTime or Date types in SQL not a string type. Your current deign unnecessarily complicates the code. If use a DateTime type in SQL then you get to format (convert to a string) the date however you like.

    https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

    https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

    I am trying to save ajax calendar text box value, such as "25/01/2024" and passing the value as parameter.

    We cannot see the value of txtDate.Text and we have no idea how you are handling culture. I recommend learning C# formatting fundamentals so you can design a more robust approach.

    https://learn.microsoft.com/en-us/dotnet/standard/base-types/parsing-datetime

    Lastly, if we assume the dates are always submitted as dd/MM/yyyy then the following will work. This is similar to the previous link.

    string myDate = "25/01/2024";
    DateTime dateTime = DateTime.ParseExact(myDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
    Console.WriteLine(dateTime.ToString("dd/MM/yyyy"));
    

    The code above will break if the date has a different format which can happen depending on the user's culture setting.

    Using TryParse is more robust. It up to you to handle a date parsing error.

    string myDate = "25/01/2024";
    DateTime myDatetimeType;
    if (!DateTime.TryParseExact(myDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out myDatetimeType))
    { 
        //There was an error converting the string to a date.
        //Write code to let the user know and/or handle the error
    }
    Console.WriteLine(myDatetimeType.ToString("dd/MM/yyyy"));
    

    https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?view=net-8.0

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.