c# - Inserting Date and time from two text boxes -


inserting date , time 2 separate text boxes. , concatenating save database. have seen lots of examples datetime conversions didn't clue. advance in

datetime datetime = dateconversion.converttodate(txtdate.text.trim()); datetime time = convert.todatetime(txttime.text); 
insert table(date) values('"+datetime+" "+time+"') 

first, never use convert.todatetime directly on user input, since it's going throw exceptions when input not convertible datetime.
instead, use datetime.tryparse or datetime.tryparseexact.

second, never concatenate strings create sql statement, since it's open door sql injections attacks. use parameterized queries instead.

your code should this:

datetime datetimevalue; string datetimestring = txtdate.text.trim() +' '+ txttime.text.trim(); if(datetime.tryparse(datetimestring, out datetimevalue)) {     using(sqlconnection con = new sqlconnection("connectionstring")) {         sqlcommand cmd = new sqlcommand("insert table(date) values(@value)", con);         cmd.parameters.add("@value", sqldbtype.datetime).value = datetimevalue;         con.open();         cmd.executenonquery();         con.close();     } } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -