Converting Long Date To Short Date Format in Linq Query
-
Sanket.Patil wrote:
So If There is any other method For This Please Reply.
Make sure those 2 fields are of type
DATETIME
when doing your SQL select.Hi J4amieC, Here is my code to review :-
private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cmbFinacialYear.DataSource = null;
cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
cmbFinacialYear.DisplayMember = "Sdate";
cmbFinacialYear.ValueMember = "Code";
cmbFinacialYear.SelectedIndex = -1;
}
catch (Exception ex)
{
objcon.WriteLog("Login Master", "btnLogin_Click", ex);
}
}Hope you will find the better Solution From these. Thanks for the Help.... Thanks & Regards -- --- ---- Sanket Patil.
.
-
Hi Richard, Here is my Code to Review :-
private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cmbFinacialYear.DataSource = null;
cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
cmbFinacialYear.DisplayMember = "Sdate";
cmbFinacialYear.ValueMember = "Code";
cmbFinacialYear.SelectedIndex = -1;
}
catch (Exception ex)
{
objcon.WriteLog("Login Master", "btnLogin_Click", ex);
}
}Hope You will get better Solution From These. Thanks For Help..... Thanks & Regard -- --- ---- Sanket Patil
.
You have posted the original code twice and in both cases you are creating
Sdate
as a concatenation of various fields includingn.CM_ST_DATE
. However this latter field will be returning its defaultToString()
method rather than the formatted version that you require. Tryn.CM_ST_DATE.ToString("D")
, or some alternateDateTime
format string to get the date only. [EDIT]above should readn.CM_ST_DATE.ToString("d")
.[/EDIT]modified on Tuesday, October 6, 2009 8:41 AM
-
You have posted the original code twice and in both cases you are creating
Sdate
as a concatenation of various fields includingn.CM_ST_DATE
. However this latter field will be returning its defaultToString()
method rather than the formatted version that you require. Tryn.CM_ST_DATE.ToString("D")
, or some alternateDateTime
format string to get the date only. [EDIT]above should readn.CM_ST_DATE.ToString("d")
.[/EDIT]modified on Tuesday, October 6, 2009 8:41 AM
Hi Richard, Thanks a lot for your help. The good news is i found the solution but not satisfied. Still the solution for now is best.Instead of getting result in "dd/MMM/yyyy" i am getting the result as "d/MM/yyyy". The Code I used for this is as Follows:- Previous Code :-
private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cmbFinacialYear.DataSource = null;
cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
cmbFinacialYear.DisplayMember = "Sdate";
cmbFinacialYear.ValueMember = "Code";
cmbFinacialYear.SelectedIndex = -1;
}
catch (Exception ex)
{
objcon.WriteLog("Login Master", "btnLogin_Click", ex);
}
}And The Code I Made Changes is :-
private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cmbFinacialYear.DataSource = null;
cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE.Day + "/" + n.CM_ST_DATE.Month + "/" + n.CM_ST_DATE.Year + " - To : " + n.CM_END_DATE.Day + "/" + n.CM_END_DATE.Month + "/" + n.CM_END_DATE.Year, Code = n.CM_CODE });
cmbFinacialYear.DisplayMember = "Sdate";
cmbFinacialYear.ValueMember = "Code";
cmbFinacialYear.SelectedIndex = -1;
}
catch (Exception ex)
{
objcon.WriteLog("Login Master", "btnLogin_Click", ex);
}
}Hope You will be satisfy From this. Thanks For Help Thanks & Regards -- --- ---- Sanket. Patil
.
-
Hi Richard, Thanks a lot for your help. The good news is i found the solution but not satisfied. Still the solution for now is best.Instead of getting result in "dd/MMM/yyyy" i am getting the result as "d/MM/yyyy". The Code I used for this is as Follows:- Previous Code :-
private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cmbFinacialYear.DataSource = null;
cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
cmbFinacialYear.DisplayMember = "Sdate";
cmbFinacialYear.ValueMember = "Code";
cmbFinacialYear.SelectedIndex = -1;
}
catch (Exception ex)
{
objcon.WriteLog("Login Master", "btnLogin_Click", ex);
}
}And The Code I Made Changes is :-
private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cmbFinacialYear.DataSource = null;
cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE.Day + "/" + n.CM_ST_DATE.Month + "/" + n.CM_ST_DATE.Year + " - To : " + n.CM_END_DATE.Day + "/" + n.CM_END_DATE.Month + "/" + n.CM_END_DATE.Year, Code = n.CM_CODE });
cmbFinacialYear.DisplayMember = "Sdate";
cmbFinacialYear.ValueMember = "Code";
cmbFinacialYear.SelectedIndex = -1;
}
catch (Exception ex)
{
objcon.WriteLog("Login Master", "btnLogin_Click", ex);
}
}Hope You will be satisfy From this. Thanks For Help Thanks & Regards -- --- ---- Sanket. Patil
.
Well I don't know why you are making things more difficult for yourself. I suggested that you use
n.CM_ST_DATE.ToString(formatstring)
(whereformatstring
is a standard or custom date format string), which will provide everything and anything you need. Take a look at the documentation here on MSDN[^] for all the possibilities. -
Well I don't know why you are making things more difficult for yourself. I suggested that you use
n.CM_ST_DATE.ToString(formatstring)
(whereformatstring
is a standard or custom date format string), which will provide everything and anything you need. Take a look at the documentation here on MSDN[^] for all the possibilities. -
Richard MacCutchan wrote:
I suggested that you use n.CM_ST_DATE.ToString(formatstring)
As did I, more than 24 hours ago. This person is incapable of reading documentation or taking advice.. therefore I gave up. I suggest you do too.
J4amieC wrote:
therefore I gave up. I suggest you do too.
I agree, very good advice. However I do live in the (somewhat vain) hope, that if I keep saying "look at MSDN", "try this", "use Google", "read a tutorial on CP" etc., some of these people may actually come to realize that they could probably solve 99% of their problems by the simple application of a little intelligence and hard work, rather than just posting questions here, then sitting back and waiting for the answer to drop in their lap.
-
Hi J4amieC, Here is my code to review :-
private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cmbFinacialYear.DataSource = null;
cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
cmbFinacialYear.DisplayMember = "Sdate";
cmbFinacialYear.ValueMember = "Code";
cmbFinacialYear.SelectedIndex = -1;
}
catch (Exception ex)
{
objcon.WriteLog("Login Master", "btnLogin_Click", ex);
}
}Hope you will find the better Solution From these. Thanks for the Help.... Thanks & Regards -- --- ---- Sanket Patil.
.
Change this line:
cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
to
cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = String.Format("From: {0:dd/MM/yyyy} - To: {1:dd/MM/yyyy}",n.CM_ST_DATE,n.CM_END_DATE), Code = n.CM_CODE });
Im not going to bother explaining why. I just want this question to die already.
-
J4amieC wrote:
therefore I gave up. I suggest you do too.
I agree, very good advice. However I do live in the (somewhat vain) hope, that if I keep saying "look at MSDN", "try this", "use Google", "read a tutorial on CP" etc., some of these people may actually come to realize that they could probably solve 99% of their problems by the simple application of a little intelligence and hard work, rather than just posting questions here, then sitting back and waiting for the answer to drop in their lap.
-
J4amieC wrote:
therefore I gave up. I suggest you do too.
I agree, very good advice. However I do live in the (somewhat vain) hope, that if I keep saying "look at MSDN", "try this", "use Google", "read a tutorial on CP" etc., some of these people may actually come to realize that they could probably solve 99% of their problems by the simple application of a little intelligence and hard work, rather than just posting questions here, then sitting back and waiting for the answer to drop in their lap.