0

In a C# Windows program, I want to update a Int field in a SQL Server table

int NewSeqno = MySeqno;    // get current sequence number

NewSeqno++;     // increment it

connection.Open();

// The following errors out saying that NewSeqno is not a column in the table.

// I want to update the field with the local variable NewSeqno.

command.CommandText = "UPDATE dbo.params SET NextSeqno = NewSeqno";

int recordsAffected = command.ExecuteNonQuery();

// The following statement, which writes a constant in the field, works fine.
command.CommandText = "UPDATE dbo.params SET NextSeqno = 123";
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    You should learn how to do [parameterized queries in C#](http://bobby-tables.com/csharp) and [why they're necessary](http://bobby-tables.com/). – mason Jul 17 '18 at 20:33
  • 2
    [You add a parameter to your `UPDATE` statement and then you bind your variable to that parameter.](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx) That link to MSDN has, as it's example, an UPDATE statement. – JNevill Jul 17 '18 at 20:34

1 Answers1

0

You have to pass the parameter to SQL query

update the command text as below -

command.CommandText = "UPDATE dbo.params SET NextSeqno = @NewSeqno";

Add this line to pass parameter

command.Parameters.Add("@NewSeqno", SqlDbType.Int).Value = NewSeqno;
Vinit
  • 2,540
  • 1
  • 15
  • 22