0

I have been trying to add a picture from the user's PC to my SQL Server database through Visual Studio using C#, but I keep getting some kind of error. Right now I am stuck with an error that says:

Non-static method requires a target

This is the code I use to get the picture from the users PC and put in in a PictureBox:

string imgLocation = "";

private void btnPretraziSliku_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = " png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|All files(*.*)|*.*";

    if (dialog.ShowDialog() == DialogResult.OK)
    {
        imgLocation = dialog.FileName.ToString();
        pbSlika.ImageLocation = imgLocation;
    }
}

This I use to add the picture in a database. Here the error occurs.

private void btnDodajSliku_Click(object sender, EventArgs e)
{
    try
    {
        byte[] images = null;
        FileStream stream = new FileStream(imgLocation, FileMode.Open, FileAccess.Read);
        BinaryReader binaryReader = new BinaryReader(stream);
        images = binaryReader.ReadBytes((int)stream.Length);

        using (var repo = new SlikaRepository())
        {
            Slika slika = new Slika { slika1 = images?.Length > 0 ? images[0] : (byte?)null };
            var slikaServis = new SlikaServices();
            slikaServis.AddSlika(slika);
        }

        MessageBox.Show("Data saved successfully.");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

This is the method in the Business Logic Layer:

public bool AddSlika(Slika slika)
{
    bool isSuccesful = false;

    using (var repo = new SlikaRepository())
    {
        int affectedRow = repo.Add(slika);
        isSuccesful = affectedRow > 0;
    }

    return isSuccesful;
}

This is the method in the Data Access Layer:

public override int Add(Slika entity, bool saveChanges = true)
{
    var oglas = Context.Oglas.SingleOrDefault(o => o.Id_oglas == entity.Ogla.Id_oglas);
    var ostecenja = Context.Oštećenja.SingleOrDefault(o => o.Id_ostecenja == entity.Oštećenja.Id_ostecenja);

    var slika = new Slika
    {
        Id_slike = entity.Id_slike,
        oglas_id = oglas.Id_oglas,
        slika1 = entity.slika1,
        //Oštećenja = ostecenja,
        ostecenje_id = entity.ostecenje_id
    };

    Entities.Add(slika);

    if (saveChanges)
    {
        return SaveChanges();
    }
    else
    {
        return 0;
    }
}

Does anybody have any idea why I'm getting this error?

Does anyone have a different, simpler way to add the picture from the users PC to the SQL Server database while still using a three-tier architecture?

Thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
GGs
  • 3
  • 3
  • 2
    Which line throws the error? – LarsTech Feb 20 '23 at 15:50
  • @LarsTech this line: var oglas = Context.Oglas.SingleOrDefault(o => o.Id_oglas == entity.Ogla.Id_oglas); – GGs Feb 20 '23 at 16:02
  • I deleted those SingleOrDefault lines now. The only error occuring now is in this: public int SaveChanges() { return Context.SaveChanges(); } Now, I know this saving method is working because I use it for other stuff too. So is it because of something else? – GGs Feb 20 '23 at 16:05
  • error: "Operand type clash: tinyint is incompatible with image" – GGs Feb 20 '23 at 16:08
  • _images[0]_ this is just the first byte of the image array, not the whole array. So it is possible that you need to pass _images_ (the whole array) – Steve Feb 20 '23 at 16:11
  • 3
    Don't use the image datatype. It has been deprecated for more than a decade (since sql server 2005). You should instead use varbinary(max). – Sean Lange Feb 20 '23 at 17:22
  • 1
    And to add onto what Sean said, don't store images in the database at all. It's a bit of an anti-pattern. Store them on disk somewhere and store the file path to that image in the database instead. – J.D. Feb 21 '23 at 04:28

0 Answers0