vendredi 22 janvier 2021

How to store and load checkbox states in database in C# with Visual Studio

I'm working on a windows forms app in Visual Studio. I have a form with a checkbox. I'd like to save the checked state in a database. And later I'd like to load the state of the checkbox from that database.

I'm using this querystring to save checkbox state into the database:

string insertQuery = "INSERT INTO database.database( checkbox_Voorschrift) VALUES('" + checkBox_VS.CheckState + "')";

In my database, I'm using a boolean.

To load the state of the checkbox, I'm using this:

checkBox_VS.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString();
  • I've tried this with textbox.Text, textbox.Checked, checkbox.CheckStatus
  • I've changed the database fieldtype to Bit, Varchar
  • I've changed the querystring to .Value

But I haven't found a working solution where for example the checked checkbox is saved to the database and later on, on loading the checkbox gets checked whem loading the form with the data from the database.

What am I doing wrong? wrong database fieldtype? Some error in the querystring,.....

thanks for sharing!!!




Aucun commentaire:

Enregistrer un commentaire