2017-10-11 3 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) 
     { 

     } 
    } 
} 

Wenn ich den Code ausführen ich auf diesen Fehler kommen, wie kann ich dies kann jemand helfen, lösen Sie bitteEine nicht behandelte Ausnahme des Typs ‚System.Data.SqlServerCe.SqlCeException‘ aufgetreten in System.Data.SqlServerCe.dll

Eine nicht behandelte Ausnahme des Typs ‚System.Data.SqlServerCe.SqlCeException‘ aufgetreten in System.Data.SqlServerCe.dll C#

Dieser Fehler kommen, wenn ich die Aufnahmetaste hinzuzufügen, drücken Sie die hinzufügen notieren Sie der Felder in der kompakten lokalen Datenbank.

+0

Wir brauchen die innere Ausnahme –

+0

Und die Zeile, in der die Ausnahme auftritt. – Shoter

+0

cmd.ExecuteNonQuery(); Ich bekomme den Fehler hier diesen Schritt nicht erfolgreich kompilieren. – Paramour

Antwort

1

Sie müssen die Namen in eckige Klammern einschließen, wenn sie Leerzeichen enthalten.

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

Beachten Sie, dass Sie auch parameterized queries zu vermeiden, SQL-Injection verwenden sollten!

Verwandte Themen