The Code
The Save Function:
public void SaveProducts()
{
TempSave _Save = new TempSave();
_Save.Localization = _LocationLabel.Text + "," + _LocNumbLab.Text;
string items = "";
for (int i = 0; i < _ProductsGrid.Rows.Count; i++)
{
items += _ProductsGrid.Rows[i].Cells[1].Value.ToString();
items += ",";
}
string Qnt = "";
for (int i = 0; i < _ProductsGrid.Rows.Count; i++)
{
Qnt += _ProductsGrid.Rows[i].Cells[0].Value.ToString();
Qnt += ",";
}
try
{
items = items.Remove(items.Length - 1, 1);
Qnt = Qnt.Remove(Qnt.Length - 1, 1);
}
catch{}
_Save.Items = items;
_Save.Quantity = Qnt;
IEnumerable<TempSave> _CheackTem;
string Cheak_TempSave = "SELECT * FROM TempSave WHERE Localization='" + _Save.Localization + "'";
using (MySqlConnection Con = new MySqlConnection("server=localhost; database=wintestbeta;user=root; password=;"))
{
Con.Open();
_CheackTem = Con.Query<TempSave>(Cheak_TempSave);
Con.Close();
}
if (_CheackTem.Count() == 0)
{
string _TempSave = "INSERT INTO TempSave (Localization,items,Quantity) VALUES('" + _Save.Localization + "','" + _Save.Items + "','" + _Save.Quantity + "')";
using (MySqlConnection Con = new MySqlConnection("server=localhost; database=wintestbeta;user=root; password=;"))
{
Con.Open();
Con.Query(_TempSave);
Con.Close();
}
}
else
{
string _TempDelete = "DELETE FROM TempSave WHERE Localization='" + _Save.Localization + "'";
using (MySqlConnection Con = new MySqlConnection("server=localhost; database=wintestbeta;user=root; password=;"))
{
Con.Open();
Con.Query(_TempDelete);
Con.Close();
}
string _TempSave = "INSERT INTO TempSave (Localization,items,Quantity) VALUES('" + _Save.Localization + "','" + _Save.Items + "','" + _Save.Quantity+ "')";
using (MySqlConnection Con = new MySqlConnection("server=localhost; database=wintestbeta;user=root; password=;"))
{
Con.Open();
Con.Query(_TempSave);
Con.Close();
}
}
}
The TempSave class
public class TempSave
{
public int id { get; set; }
public string Localization { get; set; }
public string Items { get; set; }
public string Quantity { get; set; }
}
I don't use the int id but I left it there because I can became necessary in the future
How It Works
What the code does is gather 3 information's from a grid
Get the location (this is from a label)
Get all items from a grid that have been selected and put it into a string array
Get the quantity of all items from a grid and put it into a string array
The grid looks like this
+---------------------+
|Qnt|Description|Value|
+---------------------+
Then for each string in the items string array it will join all string together with a "," between them.
Then it does the same to the quantity string array
After that it checks if in the MySQL table already contains any row in the same location
If it doesn't it will insert the row, if it does it will delete and insert the row.
This function is activated every time a new item is add to the grid no matter if it exists or not, if it exists it will just change the quantity.
I want to know if there is anyway I can make this function faster because it is taking to much time, about 2 seconds.