开发者

Cannot implicitly convert type 'object' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

I am developing my first programm and am facing some problems please help me complete it I have this code in c#:

SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.Read()) 
{
client_id = dr["clientid"].ToString();
surname = dr["surname"]开发者_StackOverflow社区.ToString();
othername = dr["othername"].ToString();
gender = dr["gender"].ToString();
date_ofbirth = dr["dateofbirth"];
nationality = dr["nationality"].ToString();
//age = dr["Age"];
residential_address = dr["residentialaddress"].ToString();
postal_address = dr["postaladdress"].ToString();
contact_number = dr["telephonenumber"].ToString();
marital_status = dr["maritalstatus"].ToString();
spouse_name = dr["spousename"].ToString();
email = dr["email"].ToString();
occupation = dr["occupation"].ToString();
typeof_id = dr["typeofid"].ToString();
id_number = dr["idnumber"].ToString();
id_expirydate = dr["idexpirydate"];
remarks = dr["remarks"].ToString();
picture = dr["picture"].ToString();
return true;
cmd.CommandText = null;
}

and the error message for this is ............... date_ofbirth = dr["dateofbirth"];

Error 2 Cannot implicitly convert type 'object' to 'System.DateTime'. An explicit conversion exists

(are you missing a cast?)

C:\Users\MICKY\Documents\Visual Studio 2008\Projects\Godswill\Godswill\Personal.cs 249 28 Godswill


You should cast all of those, rather than blindly using ToString():

date_ofbirth = (DateTime) dr["dateofbirth"];

This will "unbox" the value as needed.

Of course, an easier approach here is to use an ORM or micro-ORM (such as "dapper") - then you just run:

var user = connection.Query<User>("select * from Users where Id=@id",
         new {id = 123}).First(); // (this is using "dapper")

where User is a class with properties that match the table definition, i.e.

public class User {
    public string Surname {get;set;}
    ...
    public DateTime DateOfBirth {get;set;}
}

Also; make sure you read about using here, i.e.

using(SqlDataReader dr = cmd.ExecuteReader())
{
    if (dr.Read()) {...etc...}
}

this is even more important for connections etc, but it acts to ensure the resource is correctly Dispose()d even if there is an error. This replaces your "init as null, set to null at the end" code, and has the advantage of actually doing something ;p


When you use something like this

myVar = dr["myColumnName"];

the value of dr["myColumnName"] is seen by the compiler as a simple object. You should always cast it before assigning it that way:

myVar = (ExpectedType)dr["myColumnName"];


date_ofbirth = DateTime.Parse(dr["dateofbirth"].ToString());

or safe parse:

DateTime.TryParse(dr["dateofbirth"].ToString(), out date_ofbirth);


You will have to use Convert.ToDateTime on dr["dateofbirth"] and also on dr["idexpirydate"] (Since age would be int Convert.ToInt32 for Age in case that is failing too !)

What is returned is of type object and you will have to cast it specifically to the DataType defined, not all of them are strings so ToString() won't be the choice for all of them.

Also it would be good to check for DBNull incase you are not using nullable datatypes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜