Reputation: 21
public void ModificaNomeUtente(int slipno , int basicprice , int premium, int totalamountpaid, int weight , int totalamountbasic , int totalamountpremium , int yeildestimates , int farmercode)
{
// Creo la stringa di Connessione al DataBase
SqlConnection sqlConn = new SqlConnection(@"Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True");
// Prova
try
{
// Creo la query che andrà a modificare il nome utente
string sqlQuery = "UPDATE cottonpurchse SET slipno = '" + slipno + "' + basic price = '" + basicprice + "' + premium = '" + premium + "' + totalamountpaid = '" + totalamountpaid + "' + weight = '" + weight + "' + totalamountbasic = '" + totalamountbasic + "' + totalamountpremium = '" + totalamountpremium + "' + yeildestimated = '" + yeildestimates + "' = WHERE farmercode = '" + FarmerCode + "'";
SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
// Apro la connessione con il DataBase Login.sdf
sqlConn.Open();
// Eseguo l'istruzione sql
cmd.ExecuteNonQuery();
// Chiudo la connessione con il DataBase Login.sdf
sqlConn.Close();
// Se la query ha avuto esito positivo imposto la variabile result a true
result = true;
}
// In caso di eccezzione
catch (Exception ex)
{
// Visualizzo il messaggio con la relativa eccezzione verificatasi
MessageBox.Show(ex.Message);
// Chiudo la connessione con il DataBase Login.sdf
sqlConn.Close();
// la query ha avuto esito negativo imposto la variabile result a false
result = false;
}
finally
{
// Chiudo la connessione con il DataBase Login.sdf
sqlConn.Close();
}
}
this doesnt work its not updating could it be cause my values are set to null ?
Upvotes: 0
Views: 126
Reputation: 4779
UPDATE: everyone's comments are valid. Your SQL is full of errors: extra spaces, extra +, and extra =. You are confusing yourself trying to build up a string that way...
That said, your SQL query string should be this:
string sqlQuery = "UPDATE cottonpurchse SET slipno=" + slipno + ", basicprice=" + basicprice + ", premium=" + premium + ", totalamountpaid=" + totalamountpaid + ", weight=" + weight + ", totalamountbasic=" + totalamountbasic + ", totalamountpremium=" + totalamountpremium + ", yeildestimated=" + yeildestimates + " WHERE farmercode=" + FarmerCode;
It is confusing becuase you are building a string. The best (and safest) way to do what you are trying to do is use SQL parameters. See: c# Sqlcommand error (specifically the answer that shows "Parameters.Add")
Upvotes: 0
Reputation: 175826
That statement parses to something like;
UPDATE cottonpurchse
SET slipno = '???' + basic price = '???' + premium = '???' + totalamountpaid = '???' + weight = '???' + totalamountbasic = '???' + totalamountpremium = '???' + yeildestimated = '???' = WHERE farmercode = '???'
There is an =
before the WHERE
which is illegal, did you mean ,
instead of + ?
UPDATE cottonpurchse
SET slipno = '???' , basic price = '???' , premium = '???' , totalamountpaid = '???' , weight = '???' , totalamountbasic = '???' , totalamountpremium = '???' , yeildestimated = '???' WHERE farmercode = '???'
Upvotes: 1