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!