C Sharp And Ms Access Database Tutorial 7 How To Update Selected Datagridview Row With Textbox And Picture Box

C# And Ms Access Database Tutorial #7 – How To Update Selected Datagridview Row With Textbox And Picture Box

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #7 – How To Update Selected Datagridview Row With Textbox And Picture Box

This Tutorial Shows How To Update Selected Datagridview Row With Textbox And Picture Box In C# Windows Forms Application using visual studio 2010.

How To Update Selected Datagridview Row With Textbox And Picture Box
How To Update Selected Datagridview Row With Textbox And Picture Box

C# SOURCE CODE

        //Updating Microsoft Access Database Data
        private void button2_Click_2(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtID.Text))
            {
                MessageBox.Show("First click on datagridview cell content or Make sure id field is not empty");
            }
            else
            {
                OleDbCommand UpdateDataInMicrosoftAccessDatabaseOleDbCommand;

                //Check If One Or More Fields Are Empty

                if (String.IsNullOrEmpty(txtFullName.Text) || String.IsNullOrEmpty(txtEmailAddess.Text) || String.IsNullOrEmpty(txtPhoneNumber.Text) || String.IsNullOrEmpty(txtLanguage.Text) || String.IsNullOrEmpty(txtCountry.Text) || String.IsNullOrEmpty(txtgender.Text) || String.IsNullOrEmpty(txtImagePath.Text) || pictureBox1.Image == null)
                {
                    MessageBox.Show("One Or More Fields Are Empty Make Sure All Fields Are Filled......");
                }
                else
                {
                    try
                    {
                        UpdateDataInMicrosoftAccessDatabaseOleDbCommand = new OleDbCommand(UpdatMicrosoftAccessDatabaseDataQuery, microsoftAccessDatabaseOleDbConnection);
                        UpdateDataInMicrosoftAccessDatabaseOleDbCommand.Parameters.AddWithValue("FullName", OleDbType.VarChar).Value = txtFullName.Text;
                        UpdateDataInMicrosoftAccessDatabaseOleDbCommand.Parameters.AddWithValue("EmailAddress", OleDbType.VarChar).Value = txtEmailAddess.Text;
                        UpdateDataInMicrosoftAccessDatabaseOleDbCommand.Parameters.AddWithValue("PhoneNumber", OleDbType.VarChar).Value = txtPhoneNumber.Text;
                        UpdateDataInMicrosoftAccessDatabaseOleDbCommand.Parameters.AddWithValue("LanguageP", OleDbType.VarChar).Value = txtLanguage.Text;
                        UpdateDataInMicrosoftAccessDatabaseOleDbCommand.Parameters.AddWithValue("CountryP", OleDbType.VarChar).Value = txtCountry.Text;
                        UpdateDataInMicrosoftAccessDatabaseOleDbCommand.Parameters.AddWithValue("GenderP", OleDbType.VarChar).Value = txtgender.Text;
                        UpdateDataInMicrosoftAccessDatabaseOleDbCommand.Parameters.AddWithValue("ImagePath", OleDbType.VarChar).Value = txtImagePath.Text;
                        UpdateDataInMicrosoftAccessDatabaseOleDbCommand.Parameters.AddWithValue("ImageFile", OleDbType.Binary).Value = convertImageToByteArray(pictureBox1.Image);
                        UpdateDataInMicrosoftAccessDatabaseOleDbCommand.Parameters.AddWithValue("ID", OleDbType.Integer).Value = Convert.ToInt16(txtID.Text);
                        //Open Microsoft Access Database Connection
                        openConnection();
                        int dataInserted = UpdateDataInMicrosoftAccessDatabaseOleDbCommand.ExecuteNonQuery();

                        if (dataInserted > 0)
                        {
                            MessageBox.Show("Data Updated In Microsoft Access Database Successfully.................");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        //finally close Connection To Microsoft Access Database
                        closeConnection();
                        //Refresh DataGridView After Updating Microsoft Access Database Data
                        populateDataGridViewFromMicrosoftAccessDatabase();
                    }
                }
            }

Leave a Reply

Your email address will not be published. Required fields are marked *