0

I am really stuck an error which I tried to solve but I can't figure out the issue. All I am trying is to update record in the DB and I am getting the above error in the title.

Please help me.

Thank you in advance.

Here is my code and this is where I am getting the error imgData = File.ReadAllBytes(txtImagePath.Text);

private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                // DB Connection
                SqlConnection con = new SqlConnection(CoonectionString);
                SqlCommand cmd = new SqlCommand("Candidates.SP_Update", con);
                cmd.CommandType = CommandType.StoredProcedure;

                // Declare byte in array which holds image data
                byte[] imgData;
                //imgData = File.ReadAllBytes(EmployeePicture.ImageLocation);
                imgData = File.ReadAllBytes(txtImagePath.Text);

                // Adding data into the DB
                cmd.Parameters.AddWithValue("@CandidateID", SqlDbType.Int).Value = txtCandidateID.Text.Trim();
                cmd.Parameters.AddWithValue("@CandidateStatus", SqlDbType.VarChar).Value = cmbStatus.Text.Trim();
                cmd.Parameters.AddWithValue("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text.Trim();
                cmd.Parameters.AddWithValue("@LastName", SqlDbType.VarChar).Value = txtLastName.Text.Trim();
                cmd.Parameters.AddWithValue("@AddressLine1", SqlDbType.VarChar).Value = txtAddressLine1.Text.Trim();
                cmd.Parameters.AddWithValue("@AddressLine2", SqlDbType.VarChar).Value = txtAddressLine2.Text.Trim();
                cmd.Parameters.AddWithValue("@City", SqlDbType.VarChar).Value = txtCity.Text.Trim();
                cmd.Parameters.AddWithValue("@Telephone", SqlDbType.VarChar).Value = txtTelephone.Text.Trim();
                cmd.Parameters.AddWithValue("@Email", SqlDbType.VarChar).Value = txtEmail.Text.Trim();

                cmd.Parameters.AddWithValue("@Photos", SqlDbType.Image).Value = imgData;

                // Open DB Connection and Execute non query then close DB connection
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                // Display message for saved record
                MessageBox.Show("The information has been recorded into the database!", "Saved");
                this.Close();
            }
            catch (Exception ex)
            {
                // Display message for an exception error
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.ToString());
            }
        }
  • 1
    Have you confirmed that txtImagePath.Text has valid data? Sounds to me like it is blank. – n8wrl Apr 21 '16 at 19:48
  • The error message is pretty clear. Try stepping into your code and checking the value for `txtImagePath.Text` - it sounds like its empty. – Tim Apr 21 '16 at 19:49

0 Answers0