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)