Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Compiler Error Message: CS0029: Cannot implicitly convert type 'string' to 'System.DateTime'

Source Error:

try {                                             // Line 71: 
    mydate3 = (mydate - mydate2).ToString();
    this.Label5.Text = "Time Left: " + mydate3.ToShortTimeString();
} catch (Exception ex) {

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0266: Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)

Source Error:

foreach (object x_loopVariable in dt.Rows) {                         // Line 84:
    x= x_loopVariable  ;
    if (Convert.ToInt32(x["Selected"]) + 1 == Convert.ToInt32(x["correct"])); {
    marks += 1;
Updated 5-Nov-11 4:11am

1. You cannot use an assignment expression to convert one type to another (e.g. string to DateTime) unless the type has an operator= that will accept the source type.

2. Do what the compiler tells you.

Check your C# reference manual for further details.
Correct, a 5.
Error: "CS0029: Cannot implicitly convert type 'string' to 'System.DateTime'"

Reason : Your below code.
mydate3 = (mydate - mydate2).ToString();

I guess Type of mydate3 is DateTime, and you are trying to assign a data of Type String to it.

Resolution: Modify your code as below.
mydate3 = mydate - mydate2;

Error: "CS0266: Cannot implicitly convert type 'object' to 'int'."

Reason: Your below code. Probably you are trying to assign object of DataRow to int variable.
x= x_loopVariable ;
2011999 5-Nov-11 10:49am
mydate3 = mydate - mydate2;
not working this code
Richard MacCutchan 5-Nov-11 12:11pm
The expression mydate - mydate2 returns a TimeSpan object, not a DateTime. You need to look at the documentation on MSDN for these objects.
[no name] 6-Nov-11 1:56am
Agree, that was my bad guess. :)
Richard MacCutchan 6-Nov-11 3:20am
I do that often!
2011999 5-Nov-11 13:09pm
if (Convert.ToInt32(x["Selected"]) + 1 == Convert.ToInt32(x["correct"]))
not working this also
2011999 5-Nov-11 13:27pm
Compiler Error Message: CS0029: Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime'

Source Error:

Line 73: try
Line 74: {
Line 75: mydate3 = mydate.Subtract(mydate2);
Line 76: this.Label5.Text = "Time Left: " + mydate3.ToShortTimeString();
Line 77: }
2011999 5-Nov-11 13:34pm
foreach (object drow_loopVariable in dt.Rows) {
drow = drow_loopVariable;
if (drow("Serial") == this.Label1.Text) {
drow("Selected") = this.RadioButtonList1.SelectedIndex;
break; // TODO: might not be correct. Was : Exit For

how to write in for each
Richard MacCutchan 6-Nov-11 3:19am
I repeat: go and read i) the MSDN reference for the classes you are trying to manipulate, and ii) the C# reference manual section on classes, objects, casting and conversions. These things do not happen automatically unless the class is written to deal with it. In many cases it cannot happen because the two classes are too different.
String Gender;
    if (DDGender.SelectedItem.Text  = Convert.ToString("--SELECT--" ))
       Response.Write("Please Select the Gender");
    else if (DDGender.SelectedItem.Text = "MALE")
        Gender = DDGender.SelectedItem.Text;
        Response.Write("Select Gender" +Gender );
        Gender = DDGender.SelectedItem.Text;
        Response.Write("Select Gender" + Gender);

Clarify the error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900