Solved

Datatable

Posted on 2010-09-19
12
384 Views
Last Modified: 2012-05-10
Following function returns datatable containing two strings firstdayoflastmonth, lastdayoflastmonth

 public static DataTable returnfirstlastdateofpreviousmonth()
        {
            var Viewpreviousmonthfirstlastdate = new VPreviousmonthfirstlastdateTypedView();
            var adapterpreviousmonthfirstlastdate = new DataAccessAdapter();
            adapterpreviousmonthfirstlastdate.FetchTypedView(Viewpreviousmonthfirstlastdate.GetFieldsInfo(), Viewpreviousmonthfirstlastdate);
            var DataTablepreviousmonthfirstlastdate = new DataTable();
            DataTablepreviousmonthfirstlastdate.Clear();
            DataTablepreviousmonthfirstlastdate = Viewpreviousmonthfirstlastdate;
            return DataTablepreviousmonthfirstlastdate;
        }

I need to modify this function to return 2 outputs in date format without time for fields firstdayoflastmonth, lastdayoflastmonth
0
Comment
Question by:GRChandrashekar
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 2

Expert Comment

by:vsosu
Comment Utility
You cannot return 2 output but in the parameter you can return it.

eg.
public static DataTable returnfirstlastdateofpreviousmonth(int firstdayoflastmonth, int lastdayoflastmonth )
{
....
firstdayoflastmonth = ..
lastdayoflastmonth = ..
....
}
0
 

Author Comment

by:GRChandrashekar
Comment Utility
since it is data table i need to convert to date format and not int.
0
 
LVL 2

Expert Comment

by:vsosu
Comment Utility
I have given a sample only and you can return data table in the parameter.
0
 
LVL 2

Expert Comment

by:vsosu
Comment Utility
Use Convert.ToDateTime(..) to convert string into date format
0
 

Author Comment

by:GRChandrashekar
Comment Utility
This line
 processmonthend.Monthenddate =
                        Convert.ToDateTime(DBLastDateOfLastMonth.Rows[0]["LASTDAYOFLASTMONTH"]);

returns date and time. How do I format this to get only date ?
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
to get only date use the following format: MM/dd/yyyy

 processmonthend.Monthenddate =
                        DateTime.Parse(Convert.ToDateTime(DBLastDateOfLastMonth.Rows[0]["LASTDAYOFLASTMONTH"]).ToString("MM/dd/yyyy"));

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Expert Comment

by:vsosu
Comment Utility
You can get using tostring as below:
processmonthend.Monthenddate.ToString("dd/MM/yyyy")
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
if you want the function to return 2 outputs, instead of return datatable you can apply the following:

public static void returnfirstlastdateofpreviousmonth(out DateTime firstdayoflastmonth, out DateTime lastdayoflastmonth)
        {
            firstdayoflastmonth= DateTime.Parse(Convert.ToDateTime(DBLastDateOfLastMonth.Rows[0]["FIRSTDAYOFLASTMONTH"]).ToString("MM/dd/yyyy"));

lastdayoflastmonth = DateTime.Parse(Convert.ToDateTime(DBLastDateOfLastMonth.Rows[0]["LASTDAYOFLASTMONTH"]).ToString("MM/dd/yyyy"));
        }

Open in new window

0
 
LVL 12

Accepted Solution

by:
starlite551 earned 500 total points
Comment Utility
Perhaps this would work better :
public static void returnfirstlastdateofpreviousmonth(out DateTime firstdayoflastmonth, out DateTime lastdayoflastmonth)
        {
            firstdayoflastmonth= DateTime.Parse(Convert.ToDateTime(DBLastDateOfLastMonth.Rows[0]["FIRSTDAYOFLASTMONTH"]).ToShortDateString());

lastdayoflastmonth = DateTime.Parse(Convert.ToDateTime(DBLastDateOfLastMonth.Rows[0]["LASTDAYOFLASTMONTH"]).ToShortDateString());
        }

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
@starlite551

>>Perhaps this would work better

it ain't better, it simply different way of achieving the same result.
0
 

Expert Comment

by:huy_vvn
Comment Utility
I think ToShortDateString() is better than hard-coded format such as ToString("MM/dd/yyyy")
It's simply because ToShortDateString() uses the current culture of the running application and thus will output the better result.
i.e. on fr-FR culture, it will format as dd/MM/yyyy and on en-US, it will show MM/dd/yyyy
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
it depends in the requirements, if the value is stored in DB should be in specific format than hard-coded format is the way to go.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now