0

I have a class like defined i would like to add image to this class how can I do this:

public class CLASSFIVE
{
    public CLASSFIVE()
    {
        // Insert code required on object creation below this point.
    }

    public int ContactID { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string Contact { get; set; }
    public string GuardianPhone { get; set; }
    public string DOB { get; set; }
    public string Address { get; set; }
    public object State { get; set; }
    public string Gender { get; set; }
    public string Nationality { get; set; }
    public string Disability { get; set; }
    public object photo { get; set; }

    static string MYCONNEC = ConfigurationManager.ConnectionStrings["MYCONNECTION"].ConnectionString;

    public DataTable Select()
    {
        SqlConnection conn = new SqlConnection(MYCONNEC);
        
        DataTable dt = new DataTable();

        try
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_c5", conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(dt);

            if (conn.State != ConnectionState.Open)
            { 
                conn.Open(); 
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }

        return dt;
    }

    public bool Insert(CLASSFIVE c5)
    {
        bool isSuccess = false;

        SqlConnection conn = new SqlConnection(MYCONNEC);

        try
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO tbl_c5 (FirstName, MiddleName, LastName, Contact, GuardianPhone, DOB, Address,State, Gender, Nationality, Disability) VALUES (@FirstName, @MiddleName, @LastName, @Contact, @GuardianPhone, @DOB, @Address,@State, @Gender, @Nationality, @Disability)", conn);
            cmd.Parameters.AddWithValue("@FirstName", c5.FirstName);
            cmd.Parameters.AddWithValue("@MiddleName", c5.MiddleName);
            cmd.Parameters.AddWithValue("@LastName", c5.LastName);
            cmd.Parameters.AddWithValue("@Contact", c5.Contact);
            cmd.Parameters.AddWithValue("@GuardianPhone", c5.GuardianPhone);
            cmd.Parameters.AddWithValue("@DOB", c5.DOB);
            cmd.Parameters.AddWithValue("@Address", c5.Address);
            cmd.Parameters.AddWithValue("@State", c5.State);
            cmd.Parameters.AddWithValue("@Gender", c5.Gender);
            cmd.Parameters.AddWithValue("@Nationality", c5.Nationality);
            cmd.Parameters.AddWithValue("@Disability", c5.Disability);
            cmd.Parameters.AddWithValue("@photo", c5.Photo);

            if (conn.State != ConnectionState.Open)
            {
                conn.Open(); 
            }

            int rows = cmd.ExecuteNonQuery();

            if (rows > 0)
            {
                isSuccess = true;
            }
            else
            {
                isSuccess = false;
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }

        return isSuccess;
    }

    public bool Update(CLASSFIVE c5)
    {
        bool isSuccess = false;

        SqlConnection conn = new SqlConnection(MYCONNEC);

        try
        {
            SqlCommand cmd = new SqlCommand("UPDATE tbl_c5 SET FirstName=@FirstName, MiddleName=@MiddleName, LastName=@LastName, Contact=@Contact, GuardianPhone=@GuardianPhone, DOB=@DOB, Address=@Address, Gender=@Gender, Nationality=@Nationality, Disability=@Disability WHERE ContactID=@ContactID", conn);
            cmd.Parameters.AddWithValue("@FirstName", c5.FirstName);
            cmd.Parameters.AddWithValue("@MiddleName", c5.MiddleName);
            cmd.Parameters.AddWithValue("@LastName", c5.LastName);
            cmd.Parameters.AddWithValue("@Contact", c5.Contact);
            cmd.Parameters.AddWithValue("@GuardianPhone", c5.GuardianPhone);
            cmd.Parameters.AddWithValue("@DOB", c5.DOB);
            cmd.Parameters.AddWithValue("@Address", c5.Address);
            cmd.Parameters.AddWithValue("@State", c5.State);
            cmd.Parameters.AddWithValue("@Gender", c5.Gender);
            cmd.Parameters.AddWithValue("@Nationality", c5.Nationality);
            cmd.Parameters.AddWithValue("@Disability", c5.Disability);
            cmd.Parameters.AddWithValue("@photo", c5.Photo);
            cmd.Parameters.AddWithValue("ContactID", c5.ContactID);
                
            if (conn.State != ConnectionState.Open)
            { 
                conn.Open(); 
            }

            int rows = cmd.ExecuteNonQuery();

            if (rows > 0)
            {
                isSuccess = true;
            }
            else
            {
                isSuccess = false;
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }

        return isSuccess;
    }

    public bool Delete(CLASSFIVE c5)
    {
        bool isSuccess = false;

        SqlConnection conn = new SqlConnection(MYCONNEC);

        try
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM tbl_c5 WHERE ContactID=@ContactID", conn);

            cmd.Parameters.AddWithValue("@ContactID", c5.ContactID);

            if (conn.State != ConnectionState.Open)
            {
                conn.Open(); 
            }

            int rows = cmd.ExecuteNonQuery();

            if (rows > 0)
            {
                isSuccess = true;
            }
            else
            {
                isSuccess = false;
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }

        return isSuccess;
    }
}

And I have a save button like this in which I have referenced the class above but I would like to add and image to this button please view the code below and see if you can help me out in solving this problem:

private void BTNSAVE_Click(object sender, RoutedEventArgs e)
{
    try
    {
        if (txtFirstName.Text == "" || txtMiddelName.Text == "" || txtLastName.Text == "" || txtGuardianPhone.Text == "" || txtNationality.Text == "" || txtstate.Text == "" || txtGender.Text == "" || txtDisability.Text == "")
        {
            MessageBox.Show("Required Field:FirstName,MiddleName,LastName, \n GuardianPhone,State/Province,Gender, \n Nationality,Disability \n Save Aborted; enter accurate values");
        }
        else
        {
            using (SqlConnection conn = new SqlConnection(MYCONNEC))
            {
                c5.FirstName = txtFirstName.Text;
                c5.MiddleName = txtMiddelName.Text;
                c5.LastName = txtLastName.Text;
                c5.Contact = txtContact.Text;
                c5.GuardianPhone = txtGuardianPhone.Text;
                c5.DOB = txtDOB.Text;
                c5.Address = txtAddress.Text;
                c5.State = txtstate.Text;
                c5.Gender = txtGender.Text;
                c5.Nationality = txtNationality.Text;
                c5.Disability = txtDisability.Text;

                bool success = c5.Insert(c5);

                if (success == true)
                {
                    LBLDP.Content = "Saved Successfully!!";
                    Refresh();
                    Clear();
                }
                else
                {
                    MessageBox.Show("Contact Not Saved, Try Again");
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "MGT Message", MessageBoxButton.OK, MessageBoxImage.Error);
    }
}

Please I would like To add a c5.photo to the save button in this application, can someone help me out please?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

You can not directly save the image in the class object and in the database. To achieve this you can do this in two ways:

  1. You get the image from the request, save it to your server, and save the path of that particular file in the database. This is the best way to achieve this as it will not hit any impact on your database size.

  2. If you want to save the file in the database only then you can use the database column datatype as BLOB which allows you to save the file or lengthy object. In your class, you make the photo prop. as the string and save the base64 of the image or file (recommended) or make the property as Stream and save the file stream in this particular object and same push to database.

Rajat Nigam
  • 99
  • 2
  • 5