C# .NET - System.DateTime' does not contain a definition for 'ParseToExact'

Asked By Daniel on 14-Mar-12 09:43 PM
Hi!I am passing the date variable to a string fomat,then I am using the session to store the date in this format yyyy-mm-dd but see the code below to help fixing that.I want to convert Date from string to date format.But I am getting this error: System.DateTime' does not contain a definition for 'ParseToExact'
int newIndex = e.NewSelectedIndex;//Row that the user is selecting(!e is a parameter)
    object EventID = GridView1.DataKeys[newIndex][0].ToString();
    object stadiumID = GridView1.DataKeys[newIndex][1].ToString();
     string Game = GridView1.Rows[newIndex].Cells[0].Text;
    string Location = GridView1.Rows[newIndex].Cells[1].Text;
    string Date = GridView1.Rows[newIndex].Cells[2].Text;
    string Hour = GridView1.Rows[newIndex].Cells[3].Text;
    string Stadium = GridView1.Rows[newIndex].Cells[4].Text;
 
 
    Session["data"] = string.Format("Event:{0}<br/>Location:{1}<br/>Date:{2}<br/>Hour:{3}<br/>Stadium name:{4}<br/>", Game, Location, Date, Hour, Stadium);
    Session["StadeID"] = stadiumID;
    Session["EventID"] = EventID;
    Session["Game"] = Game;
    Session["Location"] = Location;
    Session["Date"]= DateTime.ParseToExact(Date, "HH:mm:ss", null);
    Session["Hour"] = Hour;
    Session["Stadium"] = Stadium;
[)ia6l0 iii replied to Daniel on 14-Mar-12 09:44 PM
I was stumbled for a moment. 

DateTime has Parse and ParseExact and there is no method called "ParseToExact". So change your code to, 
Session["Date"]= DateTime.ParseExact(Date, "HH:mm:ss", null);


Daniel replied to [)ia6l0 iii on 15-Mar-12 12:54 AM

I change the code like this:
 Session["Date"]= DateTime.ParseExact(Date, "HH:mm:ss", null);
 see now what I am getting :

String was not recognized as a valid DateTime.


Please could you tell me what should be the date format in the database for the date.
because the date has a date as datatype.Then in the grid view I am passing it as a string like this: string Date = GridView1.Rows[newIndex].Cells[2].Text; U can refer the code for more information bellow.

dipa ahuja replied to Daniel on 15-Mar-12 01:06 AM
ITS ParseExact NOT ParseToExact
Somesh Yadav replied to Daniel on 15-Mar-12 01:15 AM
Try this

Code:

if (details!=null && details.cableDatas.birthDate.Value.Date.CompareTo(birthDate)==0)

Reena Jain replied to Daniel on 15-Mar-12 02:10 AM
hi,

As you have told that you are converting a datetime value from string to datetime value and that value might not be a valid datetime value and hence this error or If you are fetching it from databse make sure that the retrieved value
is not null
if(ds.Tables[0].Rows[[i][1] != DBNull.Value && ds.Tables[0].Rows[i][1].ToString() != string.Empty)
{
//your code }

check that or else post the code and the value you are trying to convert
or
Try using DateTime.ParseExact.
string str="02/13/2012";
DateTime date = DateTime.ParseExact(str, "dd/MM/yyyy", null);

try above and let me know
kalpana aparnathi replied to Daniel on 15-Mar-12 05:03 AM
hi,

Try like this:

Session["Date"] = DateTime.ParseExact(Date.Text, "dd/MM/yyyy", null);

Regards,
Dilip Sharma replied to Daniel on 15-Mar-12 06:48 AM


Session["Date"]= DateTime.ParseToExact(Date,"dd/MM/yyyy",null); 

hi  u can try this ...
But u make sure .. that ur date and month should have to digit ...

Means
01 instead of 1 .....
because we are defining a format like "dd/MM/yyyy"