DateTime issue with c#

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1053 Views - Last Post: 11 July 2013 - 10:19 PM Rate Topic: -----

#1 johnedcel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-July 13

DateTime issue with c#

Posted 04 July 2013 - 08:13 PM

Hi guys, i am having a hard time fixing this problem.

public Beneficiary PushHHID(string Firstname, string Lastname, DateTime Birthday)
        {
            try {
                ClientCredentials Credentials = new ClientCredentials();
                Credentials.UserName.UserName = "username";
                Credentials.UserName.Password = "password";

                Uri OrganizationUri = new Uri("url");
                Uri HomeRealmUri = null;

                using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
                {

                    IOrganizationService service = (IOrganizationService)serviceProxy;


                    QueryExpression query = new QueryExpression
                    {
                        EntityName = "contact",
                        ColumnSet = new ColumnSet("new_householdid", "firstname", "middlename", "lastname", "new_age", "new_birthday")
                    };


                    query.Criteria.AddCondition("firstname", ConditionOperator.Equal, Firstname);
                    query.Criteria.AddCondition("lastname", ConditionOperator.Equal, Lastname);
                    query.Criteria.AddCondition("new_birthday", ConditionOperator.Equal, Birthday); 
                    
                    EntityCollection result1 = service.RetrieveMultiple(query);


                    Beneficiary beneinfo = new Beneficiary();




                    foreach (var x in result1.Entities)
                    {
                        beneinfo.HouseholdId = x.Attributes["new_householdid"].ToString();
                        beneinfo.Firstname = x.Attributes["firstname"].ToString();
                        beneinfo.Middlename = x.Attributes["middlename"].ToString();
                        beneinfo.Lastname = x.Attributes["lastname"].ToString();
                        beneinfo.Age = Convert.ToInt32(x.Attributes["new_age"]);
                        beneinfo.Birthday = DateTime.Parse(x.Attributes["new_birthday"].ToString());

                    }

                    return beneinfo;

                }



here's my problem. at first i only run my program using 2 query criteria which is the first and last name and it's working it return and display all of the information. but when i try to add birthday in the criteria it doesn't return the information. does anyone know how to fix it? TIA.

Is This A Good Question/Topic? 0
  • +

Replies To: DateTime issue with c#

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: DateTime issue with c#

Posted 04 July 2013 - 08:52 PM

I've not played with EF and DateTime's before, but if I were to guess the SQL database is storing simply a date value, while the DateTime structure contains both the date and a time likely set to midnight local time. So when the Equals comparison is used, the date read from the database maybe be being treated as a date with a time set to midnight in UTC while being compared to a date and time which has a date and time set to local time.
Was This Post Helpful? 0
  • +
  • -

#3 johnedcel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-July 13

Re: DateTime issue with c#

Posted 04 July 2013 - 08:55 PM

if that's the case. can you recommend on how will i fix it?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: DateTime issue with c#

Posted 04 July 2013 - 09:22 PM

I just realized, you aren't using EF (Entity Framework). It looks like you are using Microsoft Dynamics. Anyway, I would try using the ConditionalOperator.On instead of the ConditionalOperator.Equal. I'm just pulling that out of my @ss, though since I've never even touched Dynamics.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 johnedcel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-July 13

Re: DateTime issue with c#

Posted 04 July 2013 - 09:25 PM

it's not working. anyways, thank you for the help. wish i could finish this before the end of the day -_-
Was This Post Helpful? 0
  • +
  • -

#6 johnedcel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-July 13

Re: DateTime issue with c#

Posted 04 July 2013 - 11:56 PM

or how can i convert datetime into date only? in sql query.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: DateTime issue with c#

Posted 05 July 2013 - 05:38 AM

I wouldn't. I would just pass the DateTime as parameter in my parameterized query. http://www.dreaminco...ery-a-database/

You also may find the following article useful: http://www.databasej...-and-Ranges.htm

This post has been edited by Skydiver: 05 July 2013 - 05:48 AM

Was This Post Helpful? 0
  • +
  • -

#8 johnedcel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-July 13

Re: DateTime issue with c#

Posted 05 July 2013 - 10:54 PM

public Beneficiary PushHHID(string Firstname, string Middlename,string Lastname, string Sex, DateTime Birthday)
        {
            try
            {
                ClientCredentials Credentials = new ClientCredentials();
                Credentials.UserName.UserName = "username";
                Credentials.UserName.Password = "password";

                Uri OrganizationUri = new Uri("http://escms-crm/eSCMS/XRMServices/2011/Organization.svc");
                Uri HomeRealmUri = null;

                using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
                {

                    IOrganizationService service = (IOrganizationService)serviceProxy;


                    QueryExpression query = new QueryExpression
                    {
                        EntityName = "contact",
                        ColumnSet = new ColumnSet("new_householdid", "firstname", "middlename", "lastname", "new_age", "new_sex", "new_birthday")

                    };

                    if (Sex == "female")
                    {
                        Sex = "1";
                    }
                    else
                    {
                        Sex = "0";
                    }
                    query.Criteria.AddCondition("firstname", ConditionOperator.Equal, Firstname);
                    query.Criteria.AddCondition("middlename", ConditionOperator.Equal, Middlename);
                    query.Criteria.AddCondition("lastname", ConditionOperator.Equal, Lastname);
                    query.Criteria.AddCondition("new_sex", ConditionOperator.Equal, Sex);
                    query.Criteria.AddCondition("new_birthday", ConditionOperator.Equal, Birthday); 
                    

                    EntityCollection result1 = service.RetrieveMultiple(query);


                   Beneficiary beneinfo = new Beneficiary();

                        bool sex = false;

                        foreach (var x in result1.Entities)
                        {
                            beneinfo.HouseholdId = x.Attributes["new_householdid"].ToString();
                            beneinfo.Firstname = x.Attributes["firstname"].ToString();
                            beneinfo.Age = Convert.ToInt32(x.Attributes["new_age"]);
                            beneinfo.Middlename = x.Attributes["middlename"].ToString();
                            beneinfo.Lastname = x.Attributes["lastname"].ToString();
                            
                            sex = Convert.ToBoolean(x.Attributes["new_sex"]);
                            if (sex == false)
                            {
                                beneinfo.Sex = "male";
                            }
                            else {
                                beneinfo.Sex = "female";
                            }

                            

                            beneinfo.Birthday = x.Attributes["new_birthday"].ToString();
}



here is my whole code. and this is the result when i try to retrieve data. what really is the problem because im stuck here for 2 days now! please help!

here are my inputs:
Firstname : test
Middlename: test
Lastname: test
Sex: female

it returns all values but when i try to add birthday
Birthday : 1986-09-14 16:00:00.000 <-- this is the actual data in my database.

it doesn't return anything!

This post has been edited by johnedcel: 05 July 2013 - 10:58 PM

Was This Post Helpful? 0
  • +
  • -

#9 johnedcel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-July 13

Re: DateTime issue with c#

Posted 05 July 2013 - 11:00 PM

public Beneficiary PushHHID(string Firstname, string Middlename,string Lastname, string Sex, DateTime Birthday)
        {
            try
            {
                ClientCredentials Credentials = new ClientCredentials();
                Credentials.UserName.UserName = "username";
                Credentials.UserName.Password = "password";

                Uri OrganizationUri = new Uri("http://escms-crm/eSCMS/XRMServices/2011/Organization.svc");
                Uri HomeRealmUri = null;

                using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
                {

                    IOrganizationService service = (IOrganizationService)serviceProxy;


                    QueryExpression query = new QueryExpression
                    {
                        EntityName = "contact",
                        ColumnSet = new ColumnSet("new_householdid", "firstname", "middlename", "lastname", "new_age", "new_sex", "new_birthday")

                    };

                    if (Sex == "female")
                    {
                        Sex = "1";
                    }
                    else
                    {
                        Sex = "0";
                    }
                    query.Criteria.AddCondition("firstname", ConditionOperator.Equal, Firstname);
                    query.Criteria.AddCondition("middlename", ConditionOperator.Equal, Middlename);
                    query.Criteria.AddCondition("lastname", ConditionOperator.Equal, Lastname);
                    query.Criteria.AddCondition("new_sex", ConditionOperator.Equal, Sex);
                    query.Criteria.AddCondition("new_birthday", ConditionOperator.Equal, Birthday); 
                    

                    EntityCollection result1 = service.RetrieveMultiple(query);


                   Beneficiary beneinfo = new Beneficiary();

                        bool sex = false;

                        foreach (var x in result1.Entities)
                        {
                            beneinfo.HouseholdId = x.Attributes["new_householdid"].ToString();
                            beneinfo.Firstname = x.Attributes["firstname"].ToString();
                            beneinfo.Age = Convert.ToInt32(x.Attributes["new_age"]);
                            beneinfo.Middlename = x.Attributes["middlename"].ToString();
                            beneinfo.Lastname = x.Attributes["lastname"].ToString();
                            
                            sex = Convert.ToBoolean(x.Attributes["new_sex"]);
                            if (sex == false)
                            {
                                beneinfo.Sex = "male";
                            }
                            else {
                                beneinfo.Sex = "female";
                            }

                            

                            beneinfo.Birthday = x.Attributes["new_birthday"].ToString();
}



here is my whole code. and this is the result when i try to retrieve data. what really is the problem because im stuck here for 2 days now! please help!

here are my inputs:
Firstname : test
Middlename: test
Lastname: test
Sex: female

it returns all values but when i try to add birthday
Birthday : 1986-09-14 16:00:00.000 <-- this is the actual data in my database.

it doesn't return anything!
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: DateTime issue with c#

Posted 06 July 2013 - 05:42 AM

Duplicate threads merged. In the future don't open new threads for the same issue... especially when you are already getting help in the first thread.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: DateTime issue with c#

Posted 06 July 2013 - 07:23 AM

I think you mentioned that you tried ConditionOperator.On, but have you also tried trapping the birthday by On and OnOrBefore?
Was This Post Helpful? 0
  • +
  • -

#12 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 529
  • Joined: 10-October 09

Re: DateTime issue with c#

Posted 06 July 2013 - 10:44 AM

Try throwing
x.Attributes["new_birthday"]
into a messagebox at the beginning of the foreach loop. Also, try to use breakpoints. If the value is returning null, it may be because you need to convert it to a .Net DateTime. Look into the Convert.ToDateTime() method.

This post has been edited by optix212: 06 July 2013 - 10:51 AM

Was This Post Helpful? 0
  • +
  • -

#13 johnedcel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-July 13

Re: DateTime issue with c#

Posted 07 July 2013 - 07:38 PM

sorry for the multiple threads. the datetime issue is now fix i just change the conditionaloperator.equal to conditionaloperator.on and convert the datetime.toLocaltime() and that fix the problem. now i have another issue. when i test the program to my ide it works just fine. but when i publish it through web the Sex value is faulty. does anyone know why?
Was This Post Helpful? 0
  • +
  • -

#14 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 529
  • Joined: 10-October 09

Re: DateTime issue with c#

Posted 08 July 2013 - 05:33 AM

Well, what is happening to your sex value whenever you try to test it?
Was This Post Helpful? 0
  • +
  • -

#15 johnedcel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-July 13

Re: DateTime issue with c#

Posted 08 July 2013 - 11:47 PM

i already fix the sex value yesterday. the programs runs smoothly yesterday but today when i tried to test again. whenever i enter values on all of the fields it doesn't return anything. even if i don't enter a value it doesn't return even an error. it's maddening!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2