Click here to Skip to main content
15,395,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i have forum with picturebox and some texts so i want to insert photo to database how can i do that .

my code like so :

Models:
<pre lang="c#">namespace TestMVP.Models
{
    class OpsModel
    {
        public int CustomerID { get; set; }
        public string CustomerName { get; set; }
        public string CustomerEmail { get; set; }
        public string CustomerPhone { get; set; }
        public bool CustomerStatus { get; set; }
        public object CustomerPhoto { get; set; }

    }
}


Inetrfaces
C#
namespace TestMVP.Views.Inerfaces
{
    public interface IOps
    {
        int CustomerID { get; set; }
        string CustomerName { get; set; }
        string CustomerEmail { get; set; }
        string CustomerPhone { get; set; }
        bool CustomerStatus { get; set; }
        object DataGridView { get; set; }
        DataGridView DataGridViewRows { get; set; }
        object simpleButton2 { get; set; }
        object simpleButton3 { get; set; }
        object simpleButton4 { get; set; }
        object Cbx { get; set; }
        string CustomerDisplayMember { get; set; }
        string CustomerValueMember { get; set; }
        object CustomerPhoto { get; set; }
    }
}


Services:
C#
private static void CumstomerParamerterInsert(int CustomerID, string CustomerName, string CustomerEmail, string CustomerPhone, bool CustomerStatus, SqlCommand command)
        {
            command.Parameters.Add("@CustomerID", SqlDbType.Int).Value = CustomerID;
            command.Parameters.Add("@CustomerName", SqlDbType.NVarChar, 150).Value = CustomerName;
            command.Parameters.Add("@CustomerEmail", SqlDbType.NVarChar, 50).Value = CustomerEmail;
            command.Parameters.Add("@CustomerPhone", SqlDbType.NVarChar, 10).Value = CustomerPhone;
            command.Parameters.Add("@CustomerStatus", SqlDbType.Bit).Value = CustomerStatus;
            FrmOps f = new FrmOps();
            command.Parameters.Add("@CustomerPhoto", SqlDbType.Image).Value =Main_Classes.CLS_Photos.ImageToByteArray(f.MyPictureBox.Image);
        }

<pre>
    //This Method For Insert Customer To Database
    static public bool InsertCustomer(int CustomerID, string CustomerName, string CustomerEmail, string CustomerPhone, bool CustomerStatus)
    {
        return DBHelper.ExcuteData("Save_Customer", () => CumstomerParamerterInsert(CustomerID, CustomerName, CustomerEmail, CustomerPhone, CustomerStatus, DBHelper.command));
    }


Presenter:
C#
namespace TestMVP.Logic.Presenter
{
    class OpsPresenter
    {
        IOps iops;
        OpsModel opsModel = new OpsModel();

<pre>
    public OpsPresenter(IOps view)
    {
        iops = view;
    }

    // Connect Between Model And Interface
    private void ConnectBetweenModelAndInterface()
    {
        opsModel.CustomerID = iops.CustomerID;
        opsModel.CustomerName = iops.CustomerName;
        opsModel.CustomerEmail = iops.CustomerEmail;
        opsModel.CustomerPhone = iops.CustomerPhone;
        opsModel.CustomerStatus = iops.CustomerStatus;
        opsModel.CustomerPhoto = iops.CustomerPhoto;
    }

    // Insert Customer To Database
    public void CusInsert()
    {
        if (iops.CustomerName == "")
        {
            MessageBox.Show("Please Enter Customer Name");
            return;
        }
        if (iops.CustomerEmail == "")
        {
            MessageBox.Show("Please Enter Customer Email");
            return;
        }
        if (iops.CustomerPhone == "")
        {
            MessageBox.Show("Please Enter Customer Phone");
            return;
        }
        else
        {
            ConnectBetweenModelAndInterface();
            OpsService.InsertCustomer(opsModel.CustomerID, opsModel.CustomerName, opsModel.CustomerEmail, opsModel.CustomerPhone, opsModel.CustomerStatus);
            GetAllCus();
            AutoNumber();
            MessageBox.Show("Customer Added ... ");
        }
    }



Now In form i have Implement the object


C#
<pre>public object CustomerPhoto { get => pictureBox1.Image; set => pictureBox1.Image; }




the image not insert to database and i have change to picturebox.Initalimage but also not working


error : Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

What I have tried:

public object CustomerPhoto { get => pictureBox1.InitialImage; set => pictureBox1.Image; }


and

public object CustomerPhoto { get => pictureBox1.Image; set => pictureBox1.Image = value; }
Posted
Updated 15-Dec-20 3:34am

You are close, but your property is the problem: you have declared it as an object which means it "doesn't really have a type".
C#
public object CustomerPhoto { get => pictureBox1.Image; set => pictureBox1.Image = value; }
Change that, and it should start to work:
C#
public Image CustomerPhoto
    {
    get => pictureBox1.Image;
    set => pictureBox1.Image = value;
    }
   
Comments
khalid4775 15-Dec-20 8:52am
   
Thanks but it dosent work i change the property to Image on all classes

Interface :

Image CustomerPhoto { get; set; }


Model:

public Image CustomerPhoto { get; set; }


And Finally Impemented in the form as

public Image CustomerPhoto
{
get => pictureBox1.Image;
set => pictureBox1.Image = value;
}

not able to insert or update image
Kind Regards :)
Quote:
C#
FrmOps f = new FrmOps();
command.Parameters.Add("@CustomerPhoto", SqlDbType.Image).Value = Main_Classes.CLS_Photos.ImageToByteArray(f.MyPictureBox.Image);
You create a new, empty instance of your form class, and expect it to magically pick up the picture from a completely difference instance of the form.

Your CumstomerParamerterInsert method should have no idea about your FrpOps form. Instead, you should pass in the picture you want to save as a parameter to this method.
C#
private static void CustomerParamerterInsert(int CustomerID, string CustomerName, string CustomerEmail, string CustomerPhone, bool CustomerStatus, Image CustomerPhoto, SqlCommand command)
{
    command.Parameters.Add("@CustomerID", SqlDbType.Int).Value = CustomerID;
    command.Parameters.Add("@CustomerName", SqlDbType.NVarChar, 150).Value = CustomerName;
    command.Parameters.Add("@CustomerEmail", SqlDbType.NVarChar, 50).Value = CustomerEmail;
    command.Parameters.Add("@CustomerPhone", SqlDbType.NVarChar, 10).Value = CustomerPhone;
    command.Parameters.Add("@CustomerStatus", SqlDbType.Bit).Value = CustomerStatus;
    command.Parameters.Add("@CustomerPhoto", SqlDbType.Image).Value = Main_Classes.CLS_Photos.ImageToByteArray(CustomerPhoto);
}

static public bool InsertCustomer(int CustomerID, string CustomerName, string CustomerEmail, string CustomerPhone, bool CustomerStatus, Image CustomerPhoto)
{
    return DBHelper.ExcuteData("Save_Customer", () => CustomerParamerterInsert(CustomerID, CustomerName, CustomerEmail, CustomerPhone, CustomerStatus, CustomerPhoto, DBHelper.command));
}
C#
public interface IOps
{
    ...
    Image CustomerPhoto { get; set; }
}

class OpsModel
{
    ...
    public Image CustomerPhoto { get; set; }
}
C#
public void CusInsert()
{
    ...
    OpsService.InsertCustomer(opsModel.CustomerID, opsModel.CustomerName, opsModel.CustomerEmail, opsModel.CustomerPhone, opsModel.CustomerStatus, opsModel.CustomerPhoto);
    ...
}
C#
public Image CustomerPhoto
{
    get => this.MyPictureBox.Image;
    set => this.MyPictureBox.Image = value;
}
   
v2
Comments
khalid4775 15-Dec-20 10:26am
   
Thanks alot for help yes working fine now

but if i want get the photo from datagridview to picturebox on click event i use this method :


public void GetDataToTextBox(DataGridViewCellEventArgs e)
{
int index = e.RowIndex;// get the Row Index
DataGridViewRow selectedRow = iops.DataGridViewRows.Rows[index];
iops.CustomerID = Convert.ToInt32(selectedRow.Cells[0].Value);
iops.CustomerName = selectedRow.Cells[1].Value.ToString();
iops.CustomerEmail = selectedRow.Cells[2].Value.ToString();
iops.CustomerPhone = selectedRow.Cells[3].Value.ToString();
iops.CustomerStatus = Convert.ToBoolean(selectedRow.Cells[4].Value);
iops.CustomerPhoto = selectedRow.Cells[5].Value;
}

but there error Cannot implicitly convert type 'object' to 'System.Drawing.Image'. An explicit conversion exists (are you missing a cast?)
Richard Deeming 15-Dec-20 10:30am
   
Assuming the cell value is actually an Image, you just need to cast it:
iops.CustomerPhoto = (Image)selectedRow.Cells[5].Value;
khalid4775 15-Dec-20 10:35am
   
i tried but not working i got this error :

'Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image
Richard Deeming 15-Dec-20 10:40am
   
So the cell value is a byte[]; you'll need to cast it to a byte[]:
byte[] picture = (byte[])selectedRow.Cells[5].Value;

You can then either try to convert that to an Image, or you can change the CustomerPhoto properties and parameters to be byte[] types.
khalid4775 15-Dec-20 10:40am
   
this works correct :)

MemoryStream ms = new MemoryStream((byte[])selectedRow.Cells[5].Value);
iops.CustomerPhoto = Image.FromStream(ms);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900