Click here to Skip to main content
14,486,704 members
Rate this:
Please Sign up or sign in to vote.
See more:
i'm trying to convert date format from MM/dd/yyyy to dd/MM/yyyy. i have tried lots of thing but it is not working ..

What I have tried:

i am giving you a main code only. main codes are given below:
// cmd.Parameters.Add(DataAccess.CreateParameter(cmd, "DateofBirth", DbType.String, dob));// this is mr original code


//i'm trying to convert the date format into dd/MM/yyyy
//code here for change date format...
if (txtboxdob.Text != "")
{
    DateTime date = DateTime.ParseExact(txtboxdob.Text, "dd-MM-yyyy", null);
    cmd.Parameters.Add(DataAccess.CreateParameter(cmd, "DateofBirth", DbType.DateTime, dob));
}
else
{
    cmd.Parameters.Add(DataAccess.CreateParameter(cmd, "DateofBirth", DbType.String, txtboxdob.Text));
}
// code end here...

//but the above code is not working, it take the date format in only MM/dd/yyyy,
please tell me how can solve this problem.
Posted
Updated 19-Mar-20 3:12am
v3
Comments
Richard MacCutchan 19-Mar-20 10:38am
   
You cannot store a value in a database column as a DateTime or as a String. It can only be whatever the column is declared as. And it should never be a string for date values.
Rate this:
Please Sign up or sign in to vote.

Solution 3

You've got detailed answers here: I am trying to insert a date of birth into a database[^]

Remainder:
Date is a date, NOT its string representation!!!

Your code is is invalid, because:
1) a dob parameter is null, due to usage of date variable, instead od dob,
2) parameter name should be peceded by "@"
if (txtboxdob.Text != "")
{
    DateTime dob = DateTime.ParseExact(txtboxdob.Text, "dd-MM-yyyy", null);
    cmd.Parameters.Add(DataAccess.CreateParameter(cmd, "@DateofBirth", DbType.DateTime, dob));
}


In MS Access database engine, a name of parameter is not important, because an OleDb provider is not able to recognize the names. Important is to pass parameters in order in which they were used in a query!

So, if there is more than one parameter, check it out if they are passed in a correct order.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

You were told yesterday at I am trying to insert a date of birth into a database[^], don't store dates as text strings. And don't use TextBoxes to collect their values, use a DatePicker control which provides all the conversion methods.
   
Comments
Member 14743579 19-Mar-20 6:27am
   
i know DatePicker is easy than text but it's my task to fill date in the format of dd/MM/yyyy by using textbox.
Richard MacCutchan 19-Mar-20 6:43am
   
So why are you calling ParseExact with the format string: "dd-MM-yyyy" ?
Richard MacCutchan 19-Mar-20 7:15am
   
Changing the question without adding any details of the problem does not help. Please explain exactly what "not working" means.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
i'm trying to convert date format from MM/dd/yyyy to dd/MM/yyyy.

Why your code try to parse the date in dd-MM-yyyy format?
Separators matters, - and / are not the same.

[Update]
To store the date of birth in specific format dd-MM-yyyy, you need to convert de DateTime value to string with appropriate format and also ensure that the field with store the value is a string.
In fact the correct way to do this depend on the usage you have with the DoB.
   
v3
Comments
Member 14743579 19-Mar-20 8:04am
   
no it does not matters. this is my task to convert the format from MM/dd/yyyy to dd/MM/yyyy and nothing else.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100