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