2017-10-11 156 views
0
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlServerCe; 

namespace StaffFiles1._0 
{ 
    public partial class MainForm : Form 
    { 
     public MainForm() 
     { 
      InitializeComponent(); 
     } 

     private void fikeToolStripMenuItem_Click(object sender, EventArgs e) 
     { 

     } 

     private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 

     } 



     private void Form1_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'employee_DBDataSet1.Empoyee_GI' table. You can move, or remove it, as needed. 
      this.empoyee_GITableAdapter.Fill(this.employee_DBDataSet1.Empoyee_GI); 

     } 


     private void toolStripButton1_Click(object sender, EventArgs e) //new record button 
     { 
      NewRecordMenuItem.Enabled = false; 
      AddMenuButton.Enabled = true; 
      DeleteMenuButton.Enabled = true; 
      SaveMenuButton.Enabled = true; 

      EmployeeGroupBox.Enabled = true; 
      GenInfoGroupBox.Enabled = true; 

      name_TextBox.Focus(); 
     } 

     private void empoyee_GIListBox_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 

     private void empoyee_GIListBox_SelectedIndexChanged_1(object sender, EventArgs e) 
     { 
      if (EmpListBox.SelectedIndex == -1) 
      { 
       GenInfoGroupBox.Enabled = false; 
       EmployeeGroupBox.Enabled = false; 
       AddMenuButton.Enabled = false; 
       DeleteMenuButton.Enabled = false; 

       NewRecordMenuItem.Enabled = true; 

      } 
     } 

     private void AddMenuButton_Click(object sender, EventArgs e) 
     { 


      if (IsValidated()) 
      { 
      SqlCeConnection con = new SqlCeConnection(@"Data Source=c:\users\the doom's day\documents\visual studio 2012\Projects\StaffFiles1.0\StaffFiles1.0\Employee_DB.sdf"); 
      SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Empoyee_GI(Name,Father Name, Birthdate, Address, City, Zone, Province, Cell Number, Email, Employement Status, Hire Date, Renewal Date, Location, Position, BPS, Department, Gender, Maritial Status, CNIC, Employement Number)VALUES('"+name_TextBox.Text+"','"+father_Name_TextBox.Text+"','"+birthdate_DateTimePicker.Text+"','"+address_TextBox.Text+"','"+city_ComboBox.Text+"','"+zone_ComboBox.Text+"','"+province_ComboBox.Text+"','"+cell_Number_TextBox.Text+"','"+email_TextBox.Text+"','"+employement_Status_ComboBox.Text+"','"+hire_Date_DateTimePicker.Text+"','"+renewal_Date_DateTimePicker.Text+"','"+location_ComboBox.Text+"','"+position_ComboBox.Text+"','"+bPS_ComboBox.Text+"','"+department_ComboBox.Text+"','"+gender_ComboBox.Text+"','"+maritial_Status_ComboBox.Text+"','"+cNICTextBox.Text+"','"+employement_Number_TextBox.Text+"') ",con); 
      con.Open(); 

      cmd.ExecuteNonQuery(); 

      con.Close(); 
      MessageBox.Show("Record Added", "Working", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 


      } 



     } 

     private bool IsValidated() 
     { 
      Regex CN = new Regex(@"^[0-9]{5}-[0-9]{7}-[0-9]{1}$"); 
      Regex CL = new Regex(@"^[0-9]{4}-[0-9]{7}$"); 
      Regex EM = new Regex(@"^[a-zA-Z0-9]{1,20}@[a-zA-Z0-9]{1,20}.[a-zA-Z]{2,3}$"); 

      if (name_TextBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("Name Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
       name_TextBox.Focus(); 
       return false; 
      } 
      if (father_Name_TextBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("Father Name Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
       father_Name_TextBox.Focus(); 
       return false; 
      } 
      if (cNICTextBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("CNIC Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
       cNICTextBox.Focus(); 
       return false; 

      } 
      if (!CN.IsMatch(cNICTextBox.Text)) 
      { 
       MessageBox.Show("CNIC Format should be like 11111-1111111-1", "Format Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
       cNICTextBox.Clear(); 
       cNICTextBox.Focus(); 
       return false; 

      } 
      if (cell_Number_TextBox.Text != string.Empty) 
      { 
       if (!CL.IsMatch(cell_Number_TextBox.Text)) 
       { 
        MessageBox.Show("Cell Number Format should be like 0300-1234567", "Format Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
        cell_Number_TextBox.Clear(); 
        cell_Number_TextBox.Focus(); 
        return false; 

       } 
      } 
      if (email_TextBox.Text != string.Empty) 
      { 
       if (!EM.IsMatch(email_TextBox.Text)) 
       { 
        MessageBox.Show("Email Format should be like [email protected]", "Format Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
        email_TextBox.Clear(); 
        email_TextBox.Focus(); 
        return false; 

       } 

      } 


      if (address_TextBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("Address Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
       address_TextBox.Focus(); 
       return false; 
      } 
      if (city_ComboBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("City field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 

       return false; 
      } 
      if (province_ComboBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("Province field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 

       return false; 
      } 

      if (employement_Status_ComboBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("Status Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 

       return false; 
      } 
      if (location_ComboBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("Location Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 

       return false; 
      } 
      if (position_ComboBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("Position Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 

       return false; 
      } 
      if (bPS_ComboBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("BPS Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 

       return false; 
      } 
      if (department_ComboBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("Department Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 

       return false; 
      } 
      if (gender_ComboBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("Gender Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 

       return false; 
      } 
      if (maritial_Status_ComboBox.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("Maritial Status Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 

       return false; 
      } 


      return true; 

     } 

     private void DeleteMenuButton_Click(object sender, EventArgs e) 
     { 

     } 

     private void SaveMenuButton_Click(object sender, EventArgs e) 
     { 

     } 

     private void city_ComboBox_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 

     private void saveToolStripMenuItem_Click(object sender, EventArgs e) 
     { 

     } 

     private void tabPage1_Click(object sender, EventArgs e) 
     { 

     } 
    } 
} 

當我運行的代碼,我碰到過這樣的錯誤,我怎麼能解決這個任何一個可以幫助請型「System.Data.SqlServerCe.SqlCeException」未處理的異常發生在System.Data.SqlServerCe.dll

的類型 「System.Data.SqlServerCe.SqlCeException」未處理的異常在 System.Data.SqlServerCe.dll發生C#

此錯誤來當我按下添加記錄按鈕添加記錄 這個數據表是緊湊的本地數據庫中字段。

+0

我們需要內部異常 –

+0

併發生異常的行。 – Shoter

+0

cmd.ExecuteNonQuery(); 我在這裏得到的錯誤這一步does not成功編譯。 – Paramour

回答

1

如果包含空格,必須將方括號中的名稱包裝起來。

string sql =" 
INSERT INTO Empoyee_GI(Name,[Father Name], Birthdate, Address, City, Zone, Province, 
         [Cell Number], Email, [Employement Status], Hire Date, 
         [Renewal Date], Location, Position, BPS, Department, Gender, 
         [Maritial Status], CNIC, [Employement Number]) 
VALUES(...);"; 

SqlCeCommand cmd = new SqlCeCommand(sql,con); 

請注意,您還應該使用parameterized queries以避免sql注入!

相關問題