Riaz
Riaz

Reputation: 11

How to display only date in Label in C#

with a combo selected index changed event I am taking data from table in my labels all going well except the date. Problem: Label is showing date with 12:00 and I need only date (dd/MMM/yyyy) not with time 12:00. This date is perfect in column of table and also in gridview. Only the label shows it wrongly in the Label (AcPtRgDateLbl.Text).

here is the code:

private void AcPtPtrDd_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmd = new SqlCommand("SELECT * FROM Patients where pt_ptr='" + AcPtPtrDd.Text.ToString() + "'", con);
            con.Open();
            cmd.ExecuteNonQuery();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string idn = (string)dr["pt_name"].ToString();
                string idn1 = (string)dr["pt_date"].ToString();//Problem is here Please help to solve it.
                string idn4 = (string)dr["pt_aid"].ToString();
                string idn9 = (string)dr["pt_phone"].ToString();
                AcPtNameLbl.Text = idn;
                AcPtRgDateLbl.Text = idn1;
                AcPtAidLbl.Text = idn4;
                AcPtPhonLbl.Text = idn9;
                if (AcPtAidLbl.Text == "-")
                {
                    AcPtAidTxt.Enabled = false;
                }
                else
                {
                    AcPtAidTxt.Enabled = true;
                }
            }
            con.Close();
        }

Tabel from SQL:

ALTER procedure [dbo].[pa_getPtAccountsDataLike]
@data nvarchar(50)
as
select
p.pta_id as 'ID',
p.pta_ptr as 'PtR',
p.pta_date as 'Date',
p.pta_fee as 'Fee',
p.pta_dis as 'Discount',
p.pta_aid as 'Aid',
p.pta_rcv as 'Receive',
p.pta_bal as 'Balance'
from PtAccounts p
where
p.pta_ptr like '%'+@data+'%'
or
p.pta_date like '%'+@data+'%'
order by p.pta_ptr desc

Upvotes: 0

Views: 685

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

Try read as DateTime and then represent it as a string

string idn1 = Convert.ToDateTime(dr["pt_date"]).ToString("dd/MMM/yyyy");

More code (let's brush up your solution):

//DONE: required fields only; parametrized query
string sql = 
  @"SELECT pt_name,
           pt_date,
           pt_aid,
           pt_phone 
      FROM Patients
     WHERE pt_ptr = @pt_ptr";

//DONE: wrap IDisposable in using
using (var cmd = new SqlCommand(sql, con)) {
  //TODO: cmd.Parameters.Add("@pt_ptr", AcPtPtrDd.Text, Rdbms_Type_Here);
  // (explicit add) is a better implementation 
  cmd.Parameters.AddWithValue("@pt_ptr", AcPtPtrDd.Text);

  using (var dr = cmd.ExecuteReader()) {
    //DONE: we read at most one record only; no need in while 
    if (dr.Read()) {
      AcPtNameLbl.Text   = Convert.ToString(dr["pt_name"]);
      AcPtRgDateLbl.Text = Convert.ToDateTime(dr["pt_date"]).ToString("dd/MMM/yyyy");
      AcPtAidLbl.Text    = Convert.ToString(dr["pt_aid"]);
      AcPtPhonLbl.Text   = Convert.ToString(dr["pt_phone"]);
    }
    else {
      //DONE: what if we have an empty cursor?  
      AcPtNameLbl.Text   = "";
      AcPtRgDateLbl.Text = "";
      AcPtAidLbl.Text    = "-";
      AcPtPhonLbl.Text   = "";
    }

    AcPtAidTxt.Enabled = AcPtAidLbl.Text != "-";
  }   
} 

Upvotes: 1

Prakash
Prakash

Reputation: 863

Please try this

string idn1 = dr["pt_date"].ToString("dd/MMM/yyyy");

Upvotes: 0

Related Questions