Saturday, 31 August 2013

Update to the database error when enter more than 1 value

Update to the database error when enter more than 1 value

i have a problem, the program just read the first entered Quantity value
in the program and apply it to all rows in database and the value of first
and second row are same, even though in the beginning, first and second
row value are different.
Note: The Database 1 until 1.3 and Program 1 until 1.2 are working
properly, i just want to show you to not confuse you later. My problem is
on Database 1.5 and Program 1.4 , Database 1.6 are just wanted to show
you, the Database supposed to be like that.
First of all, my database is like this:
Product Code || Quantity (Database 1)
0001 100
0002 150
And when i run the program and entered the first "Product Code" in first
row and want to change the first Quantity value to 25, so i just enter 75
in Quantity in the program at the first row like this:
Product Code || Quantity (Program 1, 0001 is at the **first row**)
0001 75
And when i click update, the database changed to this (it works fine):
Product Code || Quantity (Database 1.1)
0001 25
0002 150
When i change the first "Product Code" in first row to second "Product
Code" in first row and want to change the second Quantity value to 100, so
i just enter 50 in Quantity in the program in the first row like this:
Product Code || Quantity (Program 1.2, 0002 still at the **first row**)
0002 50
And when i click update, the database changed to this (it works fine):
Product Code || Quantity (Database 1.3)
0001 25
0002 100
But, when i enter the first "Product Code" in first row and second
"Product code in second row in my program like this:
Product Code || Quantity (Program 1.4, 0001 at **first row** and 0002 at
**second row**)
0001 10
0002 25
And when i click update, the database changed to this (it supposed to be
like Database below):
Product Code || Quantity (Database 1.6)
0001 15
0002 85
But instead of the Database above, it changed to this (it is not working
like i want):
Product Code || Quantity (Database 1.5)
0001 15
0002 15
So, it is like the second row at the database are being ignored and
changed to the same value like in the first row, when i enter both value
at the same time in the program.
Here is the code:
private void UpdateQuantity()
{
int codeValue = 0;
int index = 0;
List<int> integers = new List<int>();
foreach (var tb in textBoxCodeContainer)
{
if (int.TryParse(tb.Text, out codeValue))
{
integers.Add(codeValue);
}
}
using (OleDbConnection conn = new
OleDbConnection(connectionString))
{
conn.Open();
string commandSelect = "SELECT [Quantity], [Description],
[Price] FROM [Seranne] WHERE [Code] = @Code";
string commandUpdate = "UPDATE [Seranne] SET [Quantity] =
@Quantity WHERE [Code] IN(" + string.Join(", ", integers)
+ ")";
using (OleDbCommand cmdSelect = new
OleDbCommand(commandSelect, conn))
using (OleDbCommand cmdUpdate = new
OleDbCommand(commandUpdate, conn))
{
cmdSelect.Parameters.Add("Code",
System.Data.OleDb.OleDbType.Integer);
cmdSelect.Parameters["Code"].Value =
this.textBoxCodeContainer[index].Text;
cmdUpdate.Parameters.Add("Quantity",
System.Data.OleDb.OleDbType.Integer);
using (OleDbDataReader dReader =
cmdSelect.ExecuteReader())
{
while (dReader.Read())
{
if (textBoxQuantityContainer[index].Value != 0)
{
newVal =
Convert.ToInt32(dReader["Quantity"].ToString())
- textBoxQuantityContainer[index].Value;
cmdUpdate.Parameters["Quantity"].Value =
newVal;
int numberOfRows =
cmdUpdate.ExecuteNonQuery();
}
index += 1;
}
dReader.Close();
}
}
conn.Close();
}
}
Could you guys please help me? Thanks.

No comments:

Post a Comment