Solved

Datatable

Posted on 2010-09-19
12
387 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to read json value 2 49
how to convert MM/dd/YYYY hh:mm:ss AM to YYYY-MM-dd datetime format by linq? 4 53
MediaHelp 4 23
C# Desktop Application 3 35
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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