عند الاضافة بالنقر على الزر لا يتم تنفيذ الامر وادخال بيانات النموذج في قاعدة البيانات ارجو المساعدة وقد تم اضافة الكلاس و الفورم والبرمجة لهما
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMySql.Data.MySqlClient;usingSystem.IO;namespaceTransparent_Form{public partial classFormEmployee:Form{private readonly EmployeeInfo _parent;
string id, name, regNo, job_Title, position, hiring_Date, country, birthdate, gender, phone, address, deptid,@img;publicFormEmployee(EmployeeInfo parent){InitializeComponent();
_parent = parent;}publicvoidUpdateInfo(){
lbltext.Text="Update Employee";
btn_Save.Text="Update";
textBox_EmpName.Text= name;
textBox_RegNo.Text= regNo;
textBox_JopTitle.Text= job_Title;
textBox_Position.Text= position;
textBox_Hiringdate.Text= hiring_Date;
textBox_Birthdate.Text= birthdate;
comboBox_Dept.Text= deptid;Cbox_Country.Text= country;
textBox_phone.Text= phone;
textBox_Address.Text= address;}publicvoidSaveInfo(){
lbltext.Text="Add Employee";
lbltext.Text="Save";}publicvoidClear(){
textBox_EmpName.Text= textBox_RegNo.Text= textBox_JopTitle.Text= textBox_Position.Text= textBox_Hiringdate.Text= textBox_Birthdate.Text= textBox_Address.Text= textBox_phone.Text=Cbox_Country.Text= comboBox_Dept.Text=String.Empty;}privatevoid panel2_Paint(object sender,PaintEventArgs e){}privatevoid button_upload_Click(object sender,EventArgs e){// browse photo from your computerOpenFileDialog opf =newOpenFileDialog();
opf.Filter="Select Photo(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif";if(opf.ShowDialog()==DialogResult.OK){
pictureBox_employee.Image=Image.FromFile(opf.FileName);
pictureBox_employee.ImageLocation= opf.FileName;
pictureBox_employee.SizeMode=PictureBoxSizeMode.StretchImage;}}privatevoid btn_Save_Click(object sender,EventArgs e){if(textBox_EmpName.Text.Trim().Length<3){MessageBox.Show("Employee name is Empty (< 3).");return;}if(textBox_RegNo.Text.Trim().Length<1){MessageBox.Show("Employee reg is Empty (< 1).");return;}if(textBox_JopTitle.Text.Trim().Length<3){MessageBox.Show("Employee title is Empty (< 3).");return;}if(textBox_Position.Text.Trim().Length<3){MessageBox.Show("Employee position is Empty (< 3).");return;}if(Cbox_Country.Text.Trim().Length<3){MessageBox.Show("Employee country is Empty (< 3).");return;}if(comboBox_Dept.Text.Trim().Length<3){MessageBox.Show("Employee Department is Empty (< 3).");return;}if(textBox_Birthdate.Text.Trim().Length<3){MessageBox.Show("Employee birthdate is Empty (< 3).");return;}if(textBox_Hiringdate.Text.Trim().Length<3){MessageBox.Show("Employee hiringdate is Empty (< 3).");return;}if(textBox_Address.Text.Trim().Length<3){MessageBox.Show("Employee Adress is Empty (< 3).");return;}if(textBox_phone.Text.Trim().Length<3){MessageBox.Show("Employee phone is Empty (< 3).");return;}if(pictureBox_employee.Image!= null);return;{}}}}
---
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceTransparent_Form{public partial classEmployeeInfo:Form{FormEmployeeForm;publicEmployeeInfo(){InitializeComponent();Form=newFormEmployee(this);}privatevoid label4_Click(object sender,EventArgs e){}publicvoidDisplay(){DbEmployee.DisplayAndSearch("SELECT* From employee_table", dataGridView);}privatevoid btn_NewEmployee_Click(object sender,EventArgs e){Form.Clear();Form.SaveInfo();Form.ShowDialog();}privatevoidEmployeeInfo_Shown(object sender,EventArgs e){Display();}privatevoid textBoxSearchEMP_TextChanged(object sender,EventArgs e){// DbEmployee.DisplayAndSearch("SELECT* From employee_table WHERE Name LIKE '%"+textBoxSearchEMP.Text +"%'", dataGridView);}privatevoid dataGridView_CellClick(object sender,DataGridViewCellEventArgs e){if(e.ColumnIndex==0){return;}if(e.ColumnIndex==1){if(MessageBox.Show("Are you want to deletE EMPLOYEE ?","Information",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Information)==DialogResult.Yes){DbEmployee.DeleteEmployee(dataGridView.Rows[e.RowIndex].Cells[2].Value.ToString());Display();}return;}}}}
----
usingMySql.Data.MySqlClient;usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMySql.Data.MySqlClient;usingSystem.IO;namespaceTransparent_Form{
internal classDbEmployee{publicstaticMySqlConnectionGetConnection(){
string sql ="datasource= localhost; port=3306;username=root;password=;database=employeedb";MySqlConnection con =newMySqlConnection(sql);try{
con.Open();}catch(MySqlException ex){MessageBox.Show("MySQL Connection! \n"+ ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);}return con;}publicstaticvoidAddEmployee(Empl emp)//{
string sql ="INSERT INTO employee_table VALUES(NULL,@EmpName,@RegNo,@Job_Tile,@Position,@Hiring_Date,@Country,@Birthdate,@Gender,@Phone,@Address,@Photo,@DeptId,NULL)";MySqlConnection con =GetConnection();MySqlCommand cmd =newMySqlCommand(sql, con);
cmd.CommandType=System.Data.CommandType.Text;
cmd.Parameters.Add("@EmpName",MySqlDbType.VarChar).Value= emp.EmpName;
cmd.Parameters.Add("@RegNo",MySqlDbType.VarChar).Value= emp.RegNo;
cmd.Parameters.Add("@job_Title",MySqlDbType.VarChar).Value= emp.Job_Title;
cmd.Parameters.Add("@position",MySqlDbType.VarChar).Value= emp.Position;
cmd.Parameters.Add("@hiring_date",MySqlDbType.VarChar).Value= emp.Hiring_Date;
cmd.Parameters.Add("@country",MySqlDbType.VarChar).Value= emp.Country;
cmd.Parameters.Add("@birthdate",MySqlDbType.VarChar).Value= emp.Birthdate;
cmd.Parameters.Add("@gender",MySqlDbType.VarChar).Value= emp.Gender;
cmd.Parameters.Add("@phone",MySqlDbType.VarChar).Value= emp.Phone;
cmd.Parameters.Add("@adrress",MySqlDbType.VarChar).Value= emp.Address;
cmd.Parameters.Add("@img",MySqlDbType.Blob).Value= emp.Photo;
cmd.Parameters.Add("@deptid",MySqlDbType.Blob).Value= emp.DeptId;try{
cmd.ExecuteNonQuery();MessageBox.Show("Added Succescfully","Infrormation",MessageBoxButtons.OK,MessageBoxIcon.Information);}catch(MySqlException ex){MessageBox.Show("Employee is not inserted. \n"+ ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);}
con.Close();}publicstaticvoidUpdateEmployee(Empl emp, string id){
string sql ="UPDATE employee_table SET Name =@EmpName,Reg No= @RegNo,Job title =@Job_Title ,Position =@position,Country =@country,Birthdate=@birthdate,Hiring Date=@hiring_date,gender=@gender ,Phone =@phone,Address=@adrress,photo=@img,DeptId=@deptid, WHERE ID - @EmpID'";MySqlConnection con =GetConnection();MySqlCommand cmd =newMySqlCommand(sql, con);
cmd.CommandType=System.Data.CommandType.Text;
cmd.Parameters.Add("id",MySqlDbType.VarChar).Value= id;
cmd.Parameters.Add("@EmpName",MySqlDbType.VarChar).Value= emp.EmpName;
cmd.Parameters.Add("@RegNo",MySqlDbType.VarChar).Value= emp.RegNo;
cmd.Parameters.Add("@Job_Title",MySqlDbType.VarChar).Value= emp.Job_Title;
cmd.Parameters.Add("@position",MySqlDbType.VarChar).Value= emp.Position;
cmd.Parameters.Add("@hiring_date",MySqlDbType.VarChar).Value= emp.Hiring_Date;
cmd.Parameters.Add("@country",MySqlDbType.VarChar).Value= emp.Country;
cmd.Parameters.Add("@birthdate",MySqlDbType.VarChar).Value= emp.Birthdate;
cmd.Parameters.Add("@gender",MySqlDbType.VarChar).Value= emp.Gender;
cmd.Parameters.Add("@phone",MySqlDbType.VarChar).Value= emp.Phone;
cmd.Parameters.Add("@adrress",MySqlDbType.VarChar).Value= emp.Address;
cmd.Parameters.Add("@img",MySqlDbType.Blob).Value= emp.Photo;try{
cmd.ExecuteNonQuery();MessageBox.Show("UPDATED Succescfully","Infrormation",MessageBoxButtons.OK,MessageBoxIcon.Information);}catch(MySqlException ex){MessageBox.Show("Employee is not UPDATED. \n"+ ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);}
con.Close();}publicstaticvoidDeleteEmployee(string id){
string sql ="DELETE FROM employee_table WHERE ID = @EmpID";MySqlConnection con =GetConnection();MySqlCommand cmd =newMySqlCommand(sql, con);
cmd.CommandType=System.Data.CommandType.Text;
cmd.Parameters.Add("@EmpID",MySqlDbType.VarChar).Value= id;try{
cmd.ExecuteNonQuery();MessageBox.Show("DELETED Succescfully","Infrormation",MessageBoxButtons.OK,MessageBoxIcon.Information);}catch(MySqlException ex){MessageBox.Show("Employee is not DELETED . \n"+ ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);}
con.Close();}publicstaticvoidDisplayAndSearch(String query,DataGridView dgv){
string sql = query;MySqlConnection con =GetConnection();MySqlCommand cmd =newMySqlCommand(sql, con);MySqlDataAdapter adp =newMySqlDataAdapter(cmd);DataTable tbl =newDataTable();
adp.Fill(tbl);
dgv.DataSource= tbl;
con.Close();}}}
السؤال
Abdu Mohamed
عند الاضافة بالنقر على الزر لا يتم تنفيذ الامر وادخال بيانات النموذج في قاعدة البيانات ارجو المساعدة وقد تم اضافة الكلاس و الفورم والبرمجة لهما
---
----
----
------
FormEmployee.cs
EmployeeInfo.cs
Empl.cs
DbEmployee.cs
5 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.