简体   繁体   中英

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!!!

There is a solution for your problem. CheckBox's CheckState is an enum type of System.Windows.Forms.CheckState so, most popular way to save an enum to database is to declare your column as SMALLINT type. You should change Boolean to SMALLINT in database. So, when you get the value from database, you have to set the CheckState like

short demoDatabaseValue = 1;
this.checkBox1.CheckState = (CheckState)demoDatabaseValue;

and your insert query should be modified to

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

However, you showed a code checkBox_VS.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString(); . This code will not work because you can not set CheckState like this. Finally, if your checkstate is in a dataGridView1 as a string then you can load state from that string like

Enum.TryParse("Checked", out CheckState myStatus); //tryparse will work for only 3 data Unchecked,Checked and Indeterminate
this.checkBox1.CheckState = myStatus;

Note: TryParse will not work for other text. And I did not checked your sql query properly. Please check the c# code and try it and let me know it works or not.

I've found this working solution:

  1. First of all, I'm storing the state of the checkbox as a SMALLINT type

  2. this is the querystring to save checkbox state into the database:

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

  3. Now I'm using this code to load the checkstate of the checkbox into the winform (as you can see, I'v' stopped using the datagridview as source):

    checkBox_VS.Checked = Convert.ToBoolean(da.GetValue(4));

Now I can load the state of the checkbox out of the database and have the boxes (un)checked in my form!

Thanks for getting me on the right track:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM