Reputation: 119
my label shows this
i don't know how to make it only view as a date without the time
because my database field is just date
CODES ASPNET:
<td>
<asp:Label ID="lbDeliveryDate" runat="server" BorderColor="Black"
Font-Size="Larger" ViewStateMode="Enabled" ></asp:Label>
</td>
CODE
void GetPO()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT PO.POID, PO.RequestedByID, PO.ShippingMethod, PO.ShippingTerm, PO.Term, PO.DeliveryDate, Users.FirstName, Users.LastName FROM PO " +
"INNER JOIN Users ON PO.RequestedByID=Users.UserID";
cmd.Parameters.AddWithValue("@UserID", Session["userid"].ToString());
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
POID.Text = dr["POID"].ToString();
lbFN.Text = dr["FirstName"].ToString();
lbLN.Text = dr["LastName"].ToString();
lbUserID.Text = dr["RequestedByID"].ToString();
lbShippingMethod.Text = dr["ShippingMethod"].ToString();
lbShippingTerm.Text = dr["ShippingTerm"].ToString();
lbPaymentTerm.Text = dr["Term"].ToString();
lbDeliveryDate.Text = dr["DeliveryDate"].ToString();
//Convert.ToDateTime(dr["DeliveryDate"].ToString());
}
//cmd.ExecuteNonQuery();
con.Close();
}
Iam getting the data DATE from the other database which i have only saved as a date field
please help
Upvotes: 0
Views: 646
Reputation: 19007
Your c# object is of Type DateTime. That's the problem. May be you can convert the value to string and then assign it to the lable.
But later on you might need to convert it to DateTime again if you have to perform any manipulations. Depends on your scenario
EDIT: you can write a c# Extension to handle the conversion of dateTime to string. Which you can reuse easily than writing up code everywhere in your application to handle Date convertion
public static class StringFormatter
{
public static string DateFormat(this string value)
{
if (value == "")
{
return "";
}
else
{
return DateTime.Parse(value.ToString()).ToString("MM/dd/yyyy");
}
}
}
Then you can do this.
lbDeliveryDate.Text = dr["DeliveryDate"].ToString().DateFormat();
Upvotes: 0
Reputation: 119
thank you guys for your time answering my question! i know this is just simple for you.
i just did as what @Arun Rana said
DateTime dt = Convert.ToDateTime(dr["DeliveryDate"].ToString());
lbDeliveryDate.Text = String.format("{0}/{1}/{2}",dt.Month ,dt.Day, dt.Year);
RATHER THAN:
lbDeliveryDate.Text = dr["DeliveryDate"].ToString();
Upvotes: 0
Reputation: 12449
As I can see you have column type Date
so it will already be in DateTime
when you will receive it in C#. You just need to use ToString()
to format it:
lbDeliveryDate.Text = dr["DeliveryDate"].ToString("d");
Upvotes: 2
Reputation: 8606
Replace your below line of code
lbDeliveryDate.Text = dr["DeliveryDate"].ToString();
with this
DateTime dt = Convert.ToDateTime(dr["DeliveryDate"].ToString());
lbDeliveryDate.Text = String.format("{0}/{1}/{2}",dt.Month ,dt.Day, dt.Year );
Upvotes: 1