Solved

Datatable

Posted on 2010-09-19
12
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 2

Expert Comment

by:vsosu
ID: 33714092
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
ID: 33714108
since it is data table i need to convert to date format and not int.
0
 
LVL 2

Expert Comment

by:vsosu
ID: 33714159
I have given a sample only and you can return data table in the parameter.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

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

Author Comment

by:GRChandrashekar
ID: 33714279
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
ID: 33714409
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
 
LVL 2

Expert Comment

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

Expert Comment

by:sedgwick
ID: 33714420
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
ID: 33714837
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
ID: 33714850
@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
ID: 33714896
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
ID: 33715097
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 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