0
Update "Batery" Set "Stock" = 10 + "Stock"  Where "idBatery" = 1 

I did execute this query in the Postgres and it worked. How do I do the query using C#.

I did try this:

"Update \"Batery\" Set \"Stock\" = " + tbAddStock.Text +"+ \"Stock\"  Where \"idBatery\" = "+ tbIdBatery.Text

uning command syntax:

"Update \"Batery\" Set \"Stock\" = (:stock) + \"Stock\"  Where \"idBatery\" = (:idbatery)"

And i get an error that say: "Syntax error in or close to <<"Stock">>", in the first case. In the cmd case i get "Cant convert an 'System.String[]' object to 'System.IConvertible'

6
  • I cheked that im sending Integer as DataType in the parameter. Commented Mar 1, 2012 at 22:14
  • 3
    and nothing is not a valid Postgres or C# error message. Commented Mar 1, 2012 at 22:16
  • Are you behind a transaction, maybe it's not being committed? Commented Mar 1, 2012 at 22:17
  • the textfield's has the data, when cmd.ExecuteNonQuery() is executed, i get that error. Commented Mar 1, 2012 at 22:24
  • ive previously added stuff in the database, and get stuff.. but this is my first update and i dont know how to do it. Commented Mar 1, 2012 at 22:25

1 Answer 1

2

Here's some sample code to illustrate using Npgsql (C# API for PostgreSQL)

1) Create a Command 2) Add parameters to the Command 3) Add values for each parameter to the command 4) Execute as a 'non query' (not expecting SQL rows returned) 5) catch your database errors

NpgsqlCommand command = new NpgsqlCommand( "update battery set stock = :stock where id = :id;", connection);
try
{
 command.Parameters.Add(new NpgsqlParameter("stock", NpgsqlTypes.NpgsqlDbType.Integer));
 command.Parameters.Add(new NpgsqlParameter("id", NpgsqlTypes.NpgsqlDbType.Integer));
 command.Parameters[0].Value = stock;
 command.Parameters[1].Value = id;

 command.ExecuteNonQuery();

command.
}
catch( NpgsqlException e )
{
....
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.