Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime Oveload

Tags:

c#

asp.net

I am trying to have this output in my label example:April 20,2016 but I get 4/20/2016:1200:00:AM instead

I tried to modified it but can't figure this error out

no overload for method takes 1 arguments cause by this code

lblDate.Text = rdr.GetValue(4).ToString("MMMM d,yyyy");

This is the entire code.

 private void GetData()
    {
        SqlConnection con = new SqlConnection("Data Source = localhost\\SQLEXPRESS;Initial Catalog = MejOnlineManagementDB00;Integrated Security=True;");
        con.Open();
        SqlCommand cmd = new SqlCommand(@"SELECT orderProdName,orderProdType,orderQuantity,orderStatus,orderDateOrdered
                                        FROM orders2
                                        WHERE orderCustomer='"+DropDownList1.SelectedItem.Value.ToString()+"'",con);
        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)
        {
            while (rdr.Read())
            {
                lblProdName.Text = rdr.GetValue(0).ToString();
                lblProdType.Text = rdr.GetValue(1).ToString();
                lblQuantity.Text = rdr.GetValue(2).ToString();
                lblStatus.Text = rdr.GetValue(3).ToString();
                lblDate.Text = rdr.GetValue(4).ToString("MMMM d,yyyy");
            }
        }
        con.Close();
    }
like image 611
rai nalasa Avatar asked Sep 28 '26 16:09

rai nalasa


1 Answers

The problem is that you are using the Object.ToString() method. This method, does not have arguments. If you want to convert it into a date, you might try this:

DateTime dt = DateTime.Parse(rdr.GetValue(4).ToString())
lblDate.Text = dt.ToString("MMMM d,yyyy");

Edit:

Please note, that if the date column in your database contains null-values, the parse methode will throw an exception. So testing for null is mandatory.

String s = Convert.ToString(rdr.GetValue(4));
if(!String.IsNullOrEmpty(s))
{
   DateTime dt = DateTime.Parse(s);
   lblDate.Text = dt.ToString("MMMM d,yyyy");
}
like image 193
ckruczek Avatar answered Sep 30 '26 05:09

ckruczek