1

i need to help me someone this checkboxs in C#. I don't know how to get the state of the check box. I will send the code to see if someone can help me with this. I want to make an Insert in the database, my query work pretty well when i test it directly in the database, and now I can't find solution for this, if any one can help me will be great.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Program_Sistem_Menaxhimi_Per_Gjykatat
{
    public partial class AddNewEmploee : Form
    {
        public AddNewEmploee()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void createUserAddNewEmploeeButt_Click(object sender, EventArgs e)
        {

        }

        private void confirmAddNewEmploeeButt_Click(object sender, EventArgs e)
        {
            string connectionString = @"Data Source=fp-PC;Initial Catalog=Gjykata;Integrated Security=True";//Connection String
            string sqlcommand = "insert into Puntoret (emri,mbiemri,adresa,telefoni,id_profesionit,isUser,isAdmin,username,passWord) values ('" + emriTextBox.Text + "','" + mbiemriTxtBox.Text + "','" + adresaTxtBox.Text + "','" + telefoniTxtBox.Text + "','" + idEProfTxtBox.Text + "','" + checkBoxUser.ThreeState.ToString() + "','" + checkBoxAdmin.ThreeState.ToString() + "','" + usernameTxtBox.Text + "','" + passwordTxtBox.Text + "')";//Sql command
            SqlConnection connection = new SqlConnection(connectionString);//Creating new Sql Connetion

            if(emriTextBox.Text != "" && mbiemriTxtBox.Text != "" && adresaTxtBox.Text != "" && telefoniTxtBox.Text != "" && idEProfTxtBox.Text != "" && checkBoxUser.ThreeState.ToString() != "False" && checkBoxAdmin.ThreeState.ToString() != "False" && usernameTxtBox.Text != "" && passwordTxtBox.Text != "")
            {
                using (SqlCommand command = new SqlCommand(sqlcommand))
                {
                    command.Connection = connection;
                    command.CommandText = sqlcommand;
                    command.CommandType = CommandType.Text;

                    try
                    {
                        connection.Open();
                        command.ExecuteNonQuery();
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }

        }
    }
}
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • 2
    Besides wondering why you're adding new butts to "emploees", where is the attempt to read the state of the checkbox? ... butt transplants? – B. Clay Shannon-B. Crow Raven Dec 03 '15 at 19:34
  • Use the `Checked` property? ie `checkBox1.Checked`. Returns `true` when checked and `false` when unchecked. – E. Moffat Dec 03 '15 at 19:36
  • 1
    The `Checked` property is what you are looking for [ToggleButton.IsChecked](https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.ischecked(v=vs.110).aspx) – AWinkle Dec 03 '15 at 19:38
  • Also, look at http://stackoverflow.com/questions/5468425/how-do-parameterized-queries-help-against-sql-injection to change your sql code. – AWinkle Dec 03 '15 at 19:39
  • Why you didn't use `checkBoxUser.Checked==True` ? – mohsen Dec 04 '15 at 03:27

2 Answers2

2

Instead of ThreeState

checkBoxUser.Checked.ToString();

will return "true" or "false"

Luca Mozzo
  • 892
  • 2
  • 8
  • 20
0

I will thank you all for your answers but I implement another solution and I think it's is better because in my database in table Puntoret(Emploees) i have two colones wich are isUser and isAdmin end those two colones at 'bit' data type wich return true or false. My implementation is in the query i write: checkBoxUser ? '1' '0' checkBoxAdmin ? '1' '0'

And in my "if statemen" is without checkBoxes. Now I'm answer to you from mobile device.. when i will have my laptop I will send full code. But again thank you all.