Hello, I'm trying to insert some data into MySQL database using Backgroundworker, now the case is the data is actually a multiple files selected by user in OpenFileDialog which I converted them to byte array before I send them to the database so in order to prevent UI (usually happens when user selected more than 3 files) from not responding I use Backgroundworker and if insert is successful then it will displays the image in picturebox.
My main is problem is lets say if the user selected more than one files it will only inserts one file into the database, and this is because the I'm trying to use the same Backgroundworker for multiple operations at the same time.
Here's my code (This will open FileDialog and let user pick image):
String getName;
String retrieved;
String varDate;
String nameTable;
Object keyVal;
int itemCurr;
String panName;
Bitmap picImage;
int curFilesCount;
private void _mainFileGenerator(int AccountType_, String _AccountTypeStr_) {
OpenFileDialog open = new OpenFileDialog();
open.Filter = "All Files|*.*|Images Files|*.jpg;*.jpeg;*.png;.bmp|";
open.Multiselect = true;
varDate = DateTime.Now.ToString("dd/MM/yyyy");
int curFilesCount = flowLayoutPanel1.Controls.Count;
if (open.ShowDialog() == DialogResult.OK) {
foreach (var selectedItems in open.FileNames) {
_filValues.Add(Path.GetFileName(selectedItems));
get_ex = open.FileName;
getName = Path.GetFileName(selectedItems);
retrieved = Path.GetExtension(selectedItems);
if (retrieved == ".png" || retrieved == ".jpeg" || retrieved == ".jpg" || retrieved == ".ico" || retrieved == ".bmp" || retrieved == ".svg") {
itemCurr++;
nameTable = "file_info";
var getImg = new Bitmap(selectedItems);
var imgWidth = getImg.Width;
var imgHeight = getImg.Height;
if (retrieved != ".ico") {
using (MemoryStream ms = new MemoryStream()) {
getImg.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
var setupImg = ms.ToArray();
keyVal = setupImg;
panName = "PanImg";
picImage = getImg;
}
}
else {
Image retrieveIcon = Image.FromFile(selectedItems);
byte[] dataIco;
using (MemoryStream msIco = new MemoryStream()) {
retrieveIcon.Save(msIco, System.Drawing.Imaging.ImageFormat.Png);
dataIco = msIco.ToArray();
}
}
}
}
}
if (backgroundWorker1.IsBusy) {
backgroundWorker1.CancelAsync();
}
else {
if (guna2ProgressBar1.Value == guna2ProgressBar1.Maximum) {
guna2ProgressBar1.Value = guna2ProgressBar1.Minimum;
}
backgroundWorker1.RunWorkerAsync(guna2ProgressBar1.Value);
}
}
Insert data (DoWork):
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
int percentFinished = (int)e.Argument;
while(!backgroundWorker1.CancellationPending && percentFinished < 100) {
percentFinished++;
backgroundWorker1.ReportProgress(percentFinished);
}
e.Result = percentFinished;
String insertTxtQuery = "INSERT INTO " + nameTable + "(CUST_FILE_NAME,UPLOAD_DATE,CUST_FILE) VALUES (@CUST_FILE_PATH,@UPLOAD_DATE,@CUST_FILE)";
command = new MySqlCommand(insertTxtQuery, con);
command.Parameters.Add("@CUST_FILE_NAME", MySqlDbType.Text);
command.Parameters.Add("@UPLOAD_DATE", MySqlDbType.VarChar, 255);
command.Parameters["@CUST_FILE_NAME"].Value = getName;
command.Parameters["@UPLOAD_DATE"].Value = varDate;
command.Parameters.Add("@CUST_FILE", MySqlDbType.LongBlob);
command.Parameters["@CUST_FILE"].Value = keyVal;
command.ExecuteNonQuery();
}
Update progress bar value (ProgressChanged):
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {
guna2ProgressBar1.Value = e.ProgressPercentage;
}
Backgroundworker completed and display image in Picturebox (RunWorkerCompleted):
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
guna2ProgressBar1.Value = 0;
int top = 275;
int h_p = 100;
var panelTxt = new Guna2Panel() {
Name = panName + itemCurr,
Width = 240,
Height = 262,
BorderRadius = 8,
FillColor = ColorTranslator.FromHtml("#121212"),
BackColor = Color.Transparent,
Location = new Point(600, top)
};
top += h_p;
flowLayoutPanel1.Controls.Add(panelTxt);
var mainPanelTxt = ((Guna2Panel)flowLayoutPanel1.Controls[panName + itemCurr]);
var textboxPic = new Guna2PictureBox();
mainPanelTxt.Controls.Add(textboxPic);
textboxPic.Name = "TxtBox" + itemCurr;
textboxPic.Width = 240;
textboxPic.Height = 164;
textboxPic.BorderRadius = 8;
textboxPic.SizeMode = PictureBoxSizeMode.CenterImage;
textboxPic.Enabled = true;
textboxPic.Visible = true;
textboxPic.Image = picImage;
Label titleLab = new Label();
mainPanelTxt.Controls.Add(titleLab);
titleLab.Name = "LabVidUp" + itemCurr;
titleLab.Font = new Font("Segoe UI Semibold", 12, FontStyle.Bold);
titleLab.ForeColor = Color.Gainsboro;
titleLab.Visible = true;
titleLab.Enabled = true;
titleLab.Location = new Point(12, 182);
titleLab.Width = 220;
titleLab.Height = 30;
titleLab.Text = getName;
Label dateLabTxt = new Label();
mainPanelTxt.Controls.Add(dateLabTxt);
dateLabTxt.Name = "LabTxtUp" + itemCurr;
dateLabTxt.Font = new Font("Segoe UI Semibold", 12, FontStyle.Bold);
dateLabTxt.ForeColor = Color.DarkGray;
dateLabTxt.Visible = true;
dateLabTxt.Enabled = true;
dateLabTxt.Location = new Point(12, 208);
dateLabTxt.Width = 1000;
dateLabTxt.Text = varDate;
}
What I have tried:
I've read a method on backgroundworker in foreach loop but none of those fixed/related to my current problem.