0

So i have a database and i am getting from it it's data by dataTableReader.

I think when you get them from the database you get them as an object, not sure, but for sure it's not the variable i set them to be in the database.

so fur I have used only int and varchar in the database so i have converted them by using toString() and int.parse(). now i need to use a datetime variable.

How can i get it from the database as a datetime or convert it to datetime and not as string(using toString())? Thanks for the Help

3
  • 1.in which format you are storing the Datetime in database? 2.does it also include time? Commented Mar 30, 2014 at 16:44
  • format datetime, it does include time but it's not important so much, i can take it off the time and leave the date only if you have a solution Commented Mar 30, 2014 at 16:47
  • check my answer below. is that you want? Commented Mar 30, 2014 at 16:51

4 Answers 4

3

Try Convert.ToDateTime(myDataReader[i]) where i is the index of your column.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, but DateTime.parse("String") did it
2

This should do in most cases:

var datetime = DateTime.Parse("string");

The string being the string saved in the database

1 Comment

@SonerGönül when a string is stored in a database it stores in usual format right? correct me if i am wrong
2

How can i get it from the database as a datetime or convert it to datetime and not as string(using toString())?

Solution 1: if you are storing the Date value as DateTime type then you can directly convert into DateTime using Convert.ToDateTime() method

Try This:

DataReader dr = new DataReader();
DateTime dt;
while(dr.Read())
{
dt = Convert.ToDateTime(dr["DateColumn"]);
}

Solution 2: If you are storing the Date in some fixed format like dd-MM-yyyy as VARCHAR then you can use DateTime.ParseExact() method

Try This:

DataReader dr = new DataReader();
DateTime dt;
while(dr.Read())
{
dt = DateTime.ParseExact(dr["DateColumn"].ToString(),"dd-MM-yyyy HH:mm:ss",
                                                 CultureInfo.InvariantCulture);
}

1 Comment

Thank you very much, but DateTime.parse("String") did it
0

try this dateTimePicker_dob.Value = Convert.ToDateTime(dr[i].ToString());

in here i is the index of your DB column and dateTimePicker_dob is your dateTimePicker Design(Name)

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.