Alex Kibler
Alex Kibler

Reputation: 4954

How can I set text inputs to 0 if the variable is null in asp-classic?

Sorry about the poorly worded title. My question is a lot longer than 150 characters so I had to try and compress it.

I've got an asp-classic page that has roughly 100 text inputs on it. These inputs are filled with data from an oracle database using adovbs. However, if the user is inserting a new record, there is obviously not going to be any data from the database, which means all of the inputs will be empty. I want all of these inputs to default to 0, but if there's any previous data from the database, I want them to have the actual data in them? How can I do this?

The current program structure is as such:

SELECT * FROM TABLE WHERE DATE=selectedDate AND SHIFT=selectedShift

<%
     date = RS("DATE")
%>

build website

<input type="text" id="date" value="<%=date%>"></input>

You get the point. How can I make it so if date is null, i.e., there's no data in the selected record, the input's value will be 0 without having to write an if statement for every single input? I'm using jquery on my page, so is there any way I can do a post check after the page is built, and just do

for each [input]
  if ([input].val.length==0)
       [input].val = 0

Is that possible? And if so, what would the syntax be for that? I'm really only good with jQuery UI and not the basic stuff.

Upvotes: 0

Views: 1310

Answers (3)

Alex Kibler
Alex Kibler

Reputation: 4954

I figured out what I needed to do. I was hoping to avoid using any oracle stuff since I don't know much about the database side of things and wanted it to just be a javascript solution if possible anyway. Here's what I ended up doing. After the page is built and the boxes are all filled from the database, any that were left blank (meaning there was no data) was autofilled with zeroes.

$('input[type="text"]').each(function() {
    if (this.value.length == 0) {
        this.value=0;
    }   
}); 

Upvotes: 0

Martha
Martha

Reputation: 3854

You could write a function that converts nulls to 0:

Function NotNull(val)
     If IsNull(val) Then
         NotNull = 0
     Else
         NotNull = val
     End If
End Function

Then, instead of...

date = RS("DATE")

... use your function to convert it to a zero if null:

date = NotNull(RS("DATE"))

Upvotes: 2

ulluoink
ulluoink

Reputation: 2785

use the isnull function in your SQL. example:

select isNull(date, 0) as date

you get a 0 when date is null

Upvotes: 2

Related Questions