Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Datatable

Posted on 2010-09-19
12
Medium Priority
?
395 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
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:Meir Rivkin
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:Meir Rivkin
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 2000 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:Meir Rivkin
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:Meir Rivkin
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

Independent Software Vendors: 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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

824 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