c# - Getting Error while Inserting the Query -
please me find error in below statements getting error in code.
create table statement
cmmd.commandtext = "create table users([user_id] autoincrement primary key, username text(50), userpwdtext(200), [isactive] yesno, [modby] long references users (user_id), [moddate] date)"; cmmd.executenonquery();
in database table created successfully.
here inserting values through code.
cmmd.commandtext = @"insert users ([username], [userpwd], [isactive], [modby], [moddate])values('admin','kov1ozykajas8awoej3oijhrqoi6q=', 'true', '1', 'datetime.now.date')"; cmmd.executenonquery();
the exception
data type mismatch in criteria expression
update:
the recommended way use parameters. using parameterized query not steps around sql injection issues, solves sql server date localization issues well.
sqlcommand cmd = new sqlcommand(@"insert users ([username], [userpwd], [isactive], [modby], [moddate])values('admin','kov1ozykajas8awoej3oijhrqoi6q=', 'true', '1', @datevalue)"); cmd.parameters.addwithvalue("@datevalue", datetime.now.date); ....... // same other.
you have 2 parentheses after values in code
check :
string format = "yyyy-mm-dd"; //or date yyyy-mm-dd hh:mm:ss cmmd.commandtext = string.format(@"insert users ([username], [userpwd], [isactive], [modby], [moddate])values('admin','kov1ozykajas8awoej3oijhrqoi6q=', 'true', '1', '{0}')", datetime.now.date.tostring(format));
also should use parameters.
Comments
Post a Comment