Click here to Skip to main content
15,892,298 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:


C#
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:



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

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Nov-11 22:48pm    
Correct, a 5.
--SA
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 ;
 
Share this answer
 
v2
Comments
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.
RaisKazi 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
C#
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 );
    }
    else
    {
        Gender = DDGender.SelectedItem.Text;
        Response.Write("Select Gender" + Gender);
    }


    }
Clarify the error
 
Share this answer
 

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