Marc Ripley
Marc Ripley

Reputation: 813

Razor VB - Pass a Null value if a Request variable is empty

I've been trying so hard to find a solution for this, but no luck. I'm fairly new to VB and SQL, but this shouldn't be too hard. I've inherited a lot of code and there's not too much room to change db attribute types or anything.

I'm trying to run an UPDATE query using parameters in Razor, like so:

    Dim updateCommand = "UPDATE [tblJobRating] SET [ProjectManagement] = @0, [ProjComments] = @1, [Schedule] = @2, [SchedComments] = @3 WHERE [JobRatingID] = @4"

All of the columns in question need INT values, but I have one exception where I need to pass it a null value (passing another number or zero won't do). Essentially a "N/A" value for the user.

I assign the variables from Post requests, like so:

Dim projMgmt = Request.Form('projMgmt')
' ...
Dim sched = Request.Form('sched')

I have the "N/A" value posting no value right now (or it can be a string and I can check for IsNumber if need be, I guess). But when I call the query execution, it enters the value as a 0.

db.Execute(updateCommand, projMgmt, projComments, sched, schedComments, ratingId)

It needs to be a NULL value for the backend to work properly. I've tried type checking and passing Nothing, System.Data.SqlTypes.SqlInt32.Null, etc., but it either gives conversion errors or sets to 0. How can I pass it properly?

Edit: I left out the first param in the db.Execute method, passing in the updateCommand. Edited for clarity.

Upvotes: 1

Views: 624

Answers (1)

Dale M
Dale M

Reputation: 2473

The problem is in your vb variable definition. I assume you have an integer, it needs to be a nullable(of integer) all the way through to the SQL. This can also be written as integer?.

Upvotes: 1

Related Questions