2015-12-03 32 views
1

我需要幫助我這個C#中的複選框。我不知道如何獲取複選框的狀態。我會發送代碼,看看是否有人可以幫助我。我想在數據庫中創建一個Insert,當我直接在數據庫中測試它時,我的查詢工作得很好,現在我找不到解決方案,如果有人能幫助我,那將會很棒。獲取C#中複選框狀態的問題#

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(); 
        } 
       } 
      } 

     } 
    } 
} 
+2

除了想知道爲什麼你要添加新的臀部「僱員」,在哪裏試圖讀取複選框的狀態?對接移植手術? –

+0

使用'Checked'屬性?即'checkBox1.Checked'。選中時返回「true」,未選中時返回「false」。 –

+1

'Checked'屬性就是你要找的[ToggleButton.IsChecked](https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.ischecked(v = vs。 110).aspx) – AWinkle

回答

2

而不是三態

checkBoxUser.Checked.ToString(); 

將返回「真」或「假」

0

我會感謝大家的答案,但我實現了另一種解決方案,我認爲這是更好,因爲在我的Puntoret(Emploees)數據庫中,我有兩個colone,isUser和isAdmin以'bit'數據類型結束這兩個colone,返回true或false。我的實現是在我寫的查詢中: checkBoxUser? '1''0' checkBoxAdmin? '1''0'

而在我的「if statemen」中沒有checkBoxes。現在我從移動設備回答你。當我有我的筆記本電腦時,我將發送完整的代碼。 但是再次感謝大家。