اذهب إلى المحتوى
  • 0

input string was not in a correct format

Abdu Mohamed

السؤال

لدي هذا الكود وعند تشفيل البرنامج تظهر رسالة الخطأ "   input string was not in a correct format" , علما بأني الحقل هو خقل رئيسي في قاعد البيانات وقرأت انه يمكن اسخدام  TryParse؟ 

Manage Employee Form 

private void button_update_Click(object sender, EventArgs e)
        {
            // update student record
             int 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 100

            int 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);
            }
            else if (verify())
            {
                try
                {
                    // to get photo from picture box
                    MemoryStream ms = new MemoryStream();
                    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 

 public bool 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 = new MySqlCommand("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();
                return true;
            }
            else
            {
                connect.closeConnect();
                return false;
            }

        }
        //Create a function to delete data
        //we need only id 
        public bool deleteEmployee(int id)
        {
            MySqlCommand command = new MySqlCommand("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();
                return true;
            }
            else
            {
                connect.closeConnect();
                return false;
            }

        }

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

هذا نص الرسالة الواردة : 

بتاريخ On 7/22/2022 at 04:13 قال Wael Aljamal:

أرجو إرفاق رسالة الخطأ كاملة، مع تحديد السطر الذي سببها و نا البيانات التي تحاول إدخالها

System.FormatException
  HResult=0x80131537
  Message=Input string was not in a correct format.
  Source=mscorlib
  StackTrace:
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.Int32.Parse(String s)
   at Transparent_Form.ManageEmployeeForm.button_update_Click(Object sender, EventArgs e) in C:\Users\Abdu Mohamed\source\repos\Student Management System\Transparent Form\ManageEmployeeForm.cs:line 116
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Transparent_Form.Program.Main() in C:\Users\Abdu Mohamed\source\repos\Student Management System\Transparent Form\Program.cs:line 19

  This exception was originally thrown at this call stack:
    [External Code]
    Transparent_Form.ManageEmployeeForm.button_update_Click(object, System.EventArgs) in ManageEmployeeForm.cs
    [External Code]
    Transparent_Form.Program.Main() in Program.cs

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...