mercredi 1 avril 2015

Database update does not work at all

I have a problem with database updating and I can't figure out what's going on there. So I want to simply swap two rows (without a certain column value). Please note that I have shortened the code. I want to swap more than one value "price". Here we go:



using (connection = new SqlConnection(connectionQuery))
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
cusIdCell = row.Cells[form1.cusIdTextbox.Text].Value.ToString();
priceCell = row.Cells[form1.priceTextboxBox.Text].Value.ToString();


So at first I grab the values



cusIdCells.Add(cusIdCell);

if (priceCell.Contains(","))
{
tempVar = priceCell.Replace(",", ".");
priceCells.Add(tempVar);
}

else
{

priceCells.Add(priceCell);

}


Now I store the values in Lists so that I get a List for each row. I think here's the problem. The cusId is an integer value. The other values are float values. Since they are in the database as for example 1.22, C# grabs them as 1,22 so that I convert them. I use the if-else statement for each value in the row (there are more than just 'price'). - xxxCell are the values in the Cells and 'xxxCells' is the list for each row.



string value1 = Convert.ToString(swapFirst.SelectedItem);
string value2 = Convert.ToString(swapSecond.SelectedItem);


Now I take the values (cusId's) which the user wants to swap



int indexValue1 = cusIdCells.IndexOf(value1);
int indexValue2 = cusIdCells.IndexOf(value2);


and take their index in the lists...



String conQuery = form1.conStringBox.Text;
SqlConnection con = new SqlConnection(conQuery);

SqlCommand cmd = new SqlCommand("UPDATE " + form1.tableName.Text + " SET " + form1.priceTextbox.Text + " = @PRICE WHERE " + form1.cusIdTextboxBox.Text + " = @CUSID", con);
cmd.Parameters.Add("@PRICE", SqlDbType.VarChar, 50).Value = priceCells[indexValue2];
cmd.Parameters.Add("@CUSID", SqlDbType.VarChar, 50).Value = cusIdCells[indexValue1];

cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();

//same here with @PRICE2, @CUSID2 and Parameters with Value = priceCells[indexValue1] and cusIdCells[indexValue2] and the three connection lines (open,,execute,close)


And establich the connection where I do the database update.


Now I don't know where the problem is. He does not crash but he does nothing. I tried it with Float since VarChar in the Parameter lines (with converting the values from "," to "." and without). I tried the same with the VarChar method and even with float values. In some cases he crashed because he can't convert float to char. Since C# recognizes the floats as "," not as "." I tried this. In other cases like creating a table, searching for values or selecting values with WHERE this seems to work fine but in this UPDATE case not. What's wrong?


Aucun commentaire:

Enregistrer un commentaire