0

I would like to add 2 variables in Razor C#.

Therefore I tried this:

var newpenpoints = result.PenaltyPoints + int.Parse(penalty);

But I think it is not working as when I try to put the figure into database:

var sql5 = "UPDATE Permit SET PenaltyPoints=@0, Disqualification = @1, LastAccidentDate = @2 WHERE CDSID = @3";
        var para1 = new{newpenpoints, disqualification, dateocc, empcdsid};
        db.Execute(sql5,para1);
        Response.Redirect("~/AccidentConviction");

There was en error:

CS0746: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

Does anyone know how can I amend my code to make it do some maths?

Thanks

Robert Groves
  • 7,574
  • 6
  • 38
  • 50
Panda
  • 71
  • 1
  • 2
  • 9

2 Answers2

0

There is an error in the code that generates the anonymous type:

new{newpenpoints, disqualification, dateocc, empcdsid};

Are you sure that all of the variables (newpenpoints, disqualification, dateocc, empcdsid) are declared in the context where the compiler error is being generated?

Ivan Karajas
  • 1,081
  • 8
  • 14
0

This is not valid code:

var para1 = new { newpenpoints, disqualification, dateocc, empcdsid };

This is trying to create an anonymous type]1 with four fields, but you are not giving them names. To create this kind of type, you must either initialize the fields with values from another object that you already have, or else name them explicitly:

var para1 = new 
{
  NewPenPoints = newpenpoints, 
  Disqualification = disqualification, 
  Dateocc = dateocc, 
  Empcdsid = empcdsid
};  

However, re-reading your sample code, it suspect what you really want is an array of objects. You don't say what db is in this case but I'm guessing that Execute takes a string and an object[] for parameters. In that case, what you want is:

var para1 = new[] { newpenpoints, disqualification, dateocc, empcdsid };

Note the []: this is creating a new array of objects with the list of values in it, e.g. para1[0] = newpenpoints, etc. I think this is what you really meant to do.

Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • `obj` in my example is just a placeholder for some other object, like a parameter, or a variable in a LINQ query, or whatever. The point is, your properties have to belong to *something* before you can use them to initialize an anonymous type. You cannot simply list a bunch of local variables. – Michael Edenfield Apr 24 '12 at 12:25