c# - How to convert a string Date from database into Integer? -


i have string date in database. did below mentioned format shows error

input string not in correct format

but when referred internet method correct not work me. let me know reason?

string str1 = "select todate employee empcode='" + code + "'";  sqldatareader dr1 = conn.query(str1);  if (dr1.read()) {     string  todate1 = dr1[0].tostring();     int todate2 =convert.toint32(todate1); } 

it sounds should using datetime column in database, @ point there's no need integers or strings:

var today = datetime.today; // or maybe use datetime.now // use parameterized sql rather string concatenations string sql = "select todate employee empcode=@empcode"; using (var conn = new sqlconnection(...)) {     conn.open();     using (var command = new sqlcommand(sql, conn))     {         command.parameters.add("@empcode", sqldbtype.varchar).value = code;         using (var reader = command.executereader())         {             while (reader.read())             {                 datetime date = reader.getdatetime(0);                 if (today > date)                 {                     //                 }             }         }     } } 

if date is being stored string , can't change (and if can, so), can use datetime.parseexact instead:

// other code before while (reader.read()) {     datetime date = datetime.parseexact(reader.getstring(0),                                         "yyyy-mm-dd", // or whatever format                                         cultureinfo.invariantculture);     if (today > date)     {         //     } } 

note in both cases, uses system local time zone. may want consider storing values in utc, , performing calculations - in case can use datetime.utcnow.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -