0

guy how can i insert the value of checkbox in access database or any database.

i tried any of this sql statement but it still give me error: OleDbException was Unhandled. Data type mismatch in criteria expression. and it's pointing to myData = myCommand.ExecuteReader();

note that allowviewpsr is a boolean type of field in ms access database or the one with YES/NO. :) chkviewpsr is mycheckbox

SQL = "UPDATE `RUsers` SET `allowviewpsr` = '" + chkviewpsr.IsChecked.Value + "' WHERE `idnum`= '" + txtblkuserid.Text  + "' AND `fullname`= '" + txtblkusername.Text + "'";

also this:

SQL = "UPDATE `RUsers` SET `allowviewpsr` = '" + chkviewpsr.IsChecked + "' WHERE `idnum`= '" + txtblkuserid.Text  + "' AND `fullname`= '" + txtblkusername.Text + "'";

and also this:

SQL = "UPDATE `RUsers` SET `allowviewpsr` = '" + chkviewpsr + "' WHERE `idnum`= '" + txtblkuserid.Text  + "' AND `fullname`= '" + txtblkusername.Text + "'";

and here's my connector:

myCommand.CommandText = SQL;
myCommand.Connection = MyNewOleDbConnection;
myAdapter.SelectCommand = myCommand;
myData = myCommand.ExecuteReader();

EDITED: hi anandkumar thanks for the quick replay i tried NonQuery but it gives same error as above

SQL = "UPDATE `RWMUsers` SET `allowviewpsr` = '" + chkviewpsr.IsChecked.Value + "' WHERE `idnum`= '" + txtblkuserid.Text  + "' AND `fullname`= '" + txtblkusername.Text + "'";
myCommand.CommandText = SQL;
myCommand.Connection = MyNewOleDbConnection;
myAdapter.UpdateCommand = myCommand;
myCommand.ExecuteNonQuery();

Snapshot of my Access Database :(

enter image description here

Kenshi Hemura
  • 135
  • 5
  • 11
  • there is some bad practice in this code ;) you should read about query injection and how to avoid it. – Felice Pollano Oct 30 '12 at 06:09
  • can you give me sample on how can i create a right sql statement... thanks :( – Kenshi Hemura Oct 30 '12 at 06:17
  • @KenshiHemura, Please upload the table constraints and column types of "RWMUsers" – andy Oct 30 '12 at 06:20
  • @KenshiHemura use Parameters to deal with variables in your sql queries. – Felice Pollano Oct 30 '12 at 07:40
  • @Felice Pollano thanks for the reply.. im sorry im not that good in c#.. can you give me an example if you dont mind. thanks :( – Kenshi Hemura Oct 30 '12 at 07:50
  • @KenshiHemura the example in the Anandkumar reply contain bot the solution for you and an example in using parameters :) – Felice Pollano Oct 30 '12 at 08:05
  • @Felice Pollano and to you also thank you so much... :) now i know how to use parameters... found very simple explanation at this blog [link](http://stackoverflow.com/questions/5893837/using-parameters-inserting-data-into-access-database) – Kenshi Hemura Oct 30 '12 at 12:56

1 Answers1

3

Instead of

myAdapter.SelectCommand = myCommand;
myCommand.ExecuteReader(); 

Use

myAdapter.UpdateCommand = myCommand;
myCommand.ExecuteNonQuery();

Reference:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand.aspx

andy
  • 5,979
  • 2
  • 27
  • 49