1

how can i save images into the database using windows presentation foundation in C# please i will be happy if given this answer. i have a class defined and filed been added but i cannot not include an image and save in database while referencing the class: i tried this:

 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;
}

but how to save into the database while referencing the class that has the above insert code is my problem i wrote a code to insert these fields like this:

 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;
                    MemoryStream ms = new MemoryStream();

                    c5.Photo = ms.ToArray();
                    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 want some one out there to explain how to add photo to this class in wpf and reference it in the save button code to save in database please i need this in a clear form (if possible you can reference a video for me please)

  • Hi, it might be easier to save the image as a file on disk, and store the path to the file in your database. Otherwise you can insert the image bytes using `SqlDbType.VarBinary` https://stackoverflow.com/questions/37472223/how-to-pass-byte-from-c-sharp-as-string-to-sql-server-stored-procedure-and-con – IronMan Nov 19 '20 at 00:51
  • please show me how to accomplish this i really need this don please ironMan –  Nov 19 '20 at 01:32
  • Does this answer your question? [how To add image To a class and save in database](https://stackoverflow.com/questions/64780233/how-to-add-image-to-a-class-and-save-in-database) –  Jan 23 '21 at 14:51

1 Answers1

0

to save image int the database in wpf application using a defined class you would have to save the image path unto a text box then read it into the database. example is shown below

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;

}

this may be your class you would like to reference. you just have to do this at the code behind button "save" event HERE:

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;
                
                **c5.ImagePath = txtimage.Text;
                c5.Photo = File.ReadAllBytes(txtimage.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);
        }
    }

NOTE: you would have to make reference at the property section of your class like this so you can bring the image path into the text box and save into in the database.

public string ImagePath { get; set; }
public byte[] Photo { get; set; }

by so doing you would be able to save the image into the database using wpf to read the byte it very simple, hope you understand the above elucidation if you have any problem you can write back.