Inserting datetime as string C# SQL-server -


i have sql-server , column called citizenshipdate. column of type datetime in sql.

however, problem can have value '0' not datetime value rather string value.

so i'm trying handle string in c# when inserting values error:

conversion failed when converting datetime character string.

i error because i'm trying insert string datetime in sql-server.

here code:

 class person {        public string personalidentitynumber { get; set; }        public string specialidentitynumber { get; set; }        public string firstname { get; set; }        public string lastname { get; set; }        public string citizenshipdate { get; set; }  }      list<folkbokforingsposttype> deserializedlist = new list<folkbokforingsposttype>();             deserializedlist = deserialize<list<folkbokforingsposttype>>();               var mypersons = deserialize<list<folkbokforingsposttype>>()                 .select(x => new person                 {                     personalidentitynumber = x.personpost.personid.personnr,                     specialidentitynumber = x.personpost.personid.tilltalsnamnsmarkering != null ? x.personpost.personid.tilltalsnamnsmarkering : null,                      lastname = x.personpost.namn.efternamn,                     firstname = x.personpost.namn.fornamn,                     citizenshipdate = x.personpost.medborgarskap != null ? x.personpost.medborgarskap.medborgarskapsdatum : null                               });                string connetionstring = null;               sqldataadapter adpter = new sqldataadapter();             dataset ds = new dataset();             xmlreader xmlfile;                connetionstring = "data source=tsrv2062;initial catalog=bums;user id=bumsuser;password=2tusen7bums";               xmlfile = xmlreader.create("navetout.xml", new xmlreadersettings());             ds.readxml(xmlfile);               using (var connection = new sqlconnection(connetionstring))             {                 connection.open();                 datetime datum = datetime.now;                 string lastchangedby = "system";                  foreach (person p in mypersons)                 {                      sqlcommand command1 = avreg(p.unregistrationreason, p.givennamenumber,p.protectedidentity, p.citizenshipdate, connection);                       command1.parameters.addwithvalue("@personalidentitynumber", string.format("{0}{1}", p.personalidentitynumber, p.specialidentitynumber));                     command1.parameters.addwithvalue("@firstname", p.firstname);                     command1.parameters.addwithvalue("@lastname", p.lastname);                     command1.executenonquery();                      console.writeline(string.format("{0}{1}", p.personalidentitynumber, p.specialidentitynumber));                   }             }              console.writeline("done");              // }// put break-point here, mouse-over personalidentitynumber...  deserializedlist contains if need            //catch (exception)            // {             //     throw;            // }             console.readkey();         }          public static sqlcommand avreg(string s, string t, string p, string c, sqlconnection connection)         {             var query = "update seamen set firstname = @firstname, "+                 "lastname = @lastname, "+              sqlcommand command1;   //here `citizenshipdate`              if (c == "0")             {                 query += ", citizenshipdate = '0'";                 command1 = new sqlcommand(query, connection);                 command1.parameters.clear();                }             else             {                 query += ", citizenshipdate = @citizenshipdate";                 command1 = new sqlcommand(query, connection);                 command1.parameters.clear();                 command1.parameters.addwithvalue("@citizenshipdate", c ?? dbnull.value.tostring());              }   //ignore these if statements              if ((!string.isnullorempty(s)) && !string.isnullorempty(t))             {              }             else             {                      query += ", givennamenumber = @givennamenumber personalidentitynumber = @personalidentitynumber";                     t = "00";                     command1 = new sqlcommand(query, connection);                     command1.parameters.clear();                     command1.parameters.addwithvalue("@givennamenumber", t ?? dbnull.value.tostring());                      return command1;              }              return command1;         } 

please note cannot change type in database. somehow need way insert value string. can ?

if column not allow nulls, can use null represent "0", , in code, when loading record, replace null "0" in ui. or, if allow nulls , need value represent "0", can use arbitrary date wouldn't use '1/1/1753' (the lowest value sql datetime) or '1/1/1900' (lowest smalldatetime) or that. date these dates represents "0". conversion "0" happens within app, not stored in database.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -