Reading a date using DataReader

TeaDrinkingGeek picture TeaDrinkingGeek · Apr 11, 2011 · Viewed 109k times · Source

I read a string using this format with a data reader. How can I read in a date using similar format?

while (MyReader.Read())
{
    TextBox1.Text = (string)MyReader["Note"];
}

Answer

Sukhjeevan picture Sukhjeevan · Apr 11, 2011

Try as given below:

while (MyReader.Read())
{
    TextBox1.Text = Convert.ToDateTime(MyReader["DateField"]).ToString("dd/MM/yyyy");
}

in ToString() method you can change data format as per your requirement.