لدي هذا الكود وعند تشفيل البرنامج تظهر رسالة الخطأ " input string was not in a correct format" , علما بأني الحقل هو خقل رئيسي في قاعد البيانات وقرأت انه يمكن اسخدام TryParse؟
Manage Employee Form
privatevoid button_update_Click(object sender,EventArgs e){// update student recordint id =Convert.ToInt32(textBox_id.Text);
string name = textBox_EmpName.Text;
string title = textBoxJob_Title.Text;
string position = textBoxPosition.Text;DateTime hdate = dateTimePicker_hd.Value;
string country = textBoxCountry.Text;DateTime bdate = dateTimePicker_bd.Value;
string phone = textBox_phone.Text;
string address = textBox_address.Text;
string gender = radioButton_male.Checked?"Male":"Female";//we need to check student age between 10 and 100int born_year = dateTimePicker_bd.Value.Year;int this_year =DateTime.Now.Year;if((this_year - born_year)<10||(this_year - born_year)>100){MessageBox.Show("The employee age must be between 10 and 100","Invalid Birthdate",MessageBoxButtons.OK,MessageBoxIcon.Error);}elseif(verify()){try{// to get photo from picture boxMemoryStream ms =newMemoryStream();
pictureBox_employee.Image.Save(ms, pictureBox_employee.Image.RawFormat);
byte[] img = ms.ToArray();if(employee.updateEmployee(id,name, title, position, hdate, country, bdate, gender, phone, address, img)){
showTable();MessageBox.Show("Employee data update","Update Employee",MessageBoxButtons.OK,MessageBoxIcon.Information);
button_clear.PerformClick();}}catch(Exception ex){MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);}}else{MessageBox.Show("Empty Field","Update Employee",MessageBoxButtons.OK,MessageBoxIcon.Warning);}}
Employee class
publicbool updateEmployee(int id, string name, string title, string position,DateTime hdate, string country,DateTime bdate, string gender, string phone, string address, byte[] img){MySqlCommand command =newMySqlCommand("UPDATE `employee` SET `EmpName`=@n,`Job_Title`=@jt,'Position'=@p,'Hiring_Date'=@hd,'Country'=@c,`Birthdate`=@bd,`Gender`=@gd,`Phone`=@ph,`Address`=@adr,`Photo`=@img WHERE `EmpId`= @id", connect.getconnection);//@id,@fn, @ln, @bd, @gd, @ph, @adr, @img
command.Parameters.Add("@id",MySqlDbType.Int32).Value= id;
command.Parameters.Add("@n",MySqlDbType.VarChar).Value= name;
command.Parameters.Add("@jt",MySqlDbType.VarChar).Value= title;
command.Parameters.Add("@p",MySqlDbType.VarChar).Value= position;
command.Parameters.Add("@hd",MySqlDbType.VarChar).Value= hdate;
command.Parameters.Add("@c",MySqlDbType.VarChar).Value= country;
command.Parameters.Add("@bd",MySqlDbType.VarChar).Value= bdate;
command.Parameters.Add("@gd",MySqlDbType.VarChar).Value= gender;
command.Parameters.Add("@ph",MySqlDbType.VarChar).Value= phone;
command.Parameters.Add("@adr",MySqlDbType.VarChar).Value= address;
command.Parameters.Add("@img",MySqlDbType.Blob).Value= img;
connect.openConnect();if(command.ExecuteNonQuery()==1){
connect.closeConnect();returntrue;}else{
connect.closeConnect();returnfalse;}}//Create a function to delete data//we need only id publicbool deleteEmployee(int id){MySqlCommand command =newMySqlCommand("DELETE FROM `employee` WHERE `EmpId`=@id", connect.getconnection);//@id
command.Parameters.Add("@id",MySqlDbType.Int32).Value= id;
connect.openConnect();if(command.ExecuteNonQuery()==1){
connect.closeConnect();returntrue;}else{
connect.closeConnect();returnfalse;}}
السؤال
Abdu Mohamed
لدي هذا الكود وعند تشفيل البرنامج تظهر رسالة الخطأ " input string was not in a correct format" , علما بأني الحقل هو خقل رئيسي في قاعد البيانات وقرأت انه يمكن اسخدام TryParse؟
Manage Employee Form
Employee class
2 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.