Solved

Populating a checkbox with info from a database - asp classic

Posted on 2015-01-21
13
115 Views
Last Modified: 2015-01-21
Below is a single line of code that I am trying to work. I want it to pull the information from the database and show the checkbox as checked.

<tr><td><b>Ninite:</b></td><td><input type="checkbox" name="cbxNinite" id="cbxNinite"<%If rs1("ninite") = "on" Then Response.Write "checked"%>><%response.write rs1("ninite")%> </td></tr>
<tr><td><b>Ninite:</b></td><td><input type="checkbox" name="cbxNinite" id="cbxNinite"checked </td></tr>

Open in new window


At the end of the first line of code I tested whether the rs1("ninite") was working. see image below. Doesn't make sense why the checkbox wont check. The second line of code is what it should do.

screenshot
0
Comment
Question by:princeservice
  • 6
  • 6
13 Comments
 
LVL 32

Accepted Solution

by:
Big Monty earned 350 total points
ID: 40562657
your html is invalid, try this:

<tr><td><b>Ninite:</b></td><td><input type="checkbox" name="cbxNinite" id="cbxNinite"<%If rs1("ninite") = "on" Then Response.Write "checked"%>><%response.write rs1("ninite")%> </td></tr>
<tr><td><b>Ninite:</b></td><td><input type="checkbox" name="cbxNinite" id="cbxNinite" checked /></td></tr>

Open in new window

0
 

Author Comment

by:princeservice
ID: 40562666
You didn't change anything.
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40562673
yes, indeed I did...

your html for the second checkbox was

<input type="checkbox" name="cbxNinite" id="cbxNinite"checked

I changed it to

<input type="checkbox" name="cbxNinite" id="cbxNinite" checked />
0
 

Author Comment

by:princeservice
ID: 40562681
That's not the problem. The second line of code is just to show what it would look like if the code:

<%If rs1("ninite") = "on" Then Response.Write "checked"%>

Open in new window



from the first line actually worked.

You can also see from the screenshot that the space does not matter.
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40562704
i always eliminate obvious html validation errors if I see them before diving deeper into an issue, that's why I pointed it out. once you get that straightened out and have verified that it wasn't an issue, we can move forward...

I should also point out that you cannot have two checkboxes with the same ID, you'll run into issues if you try to perform any javascript on them.

for your first checkbox, try:

<tr><td><b>Ninite:</b></td><td><input type="checkbox" name="cbxNinite" id="cbxNinite" <%If CStr( rs1("ninite") ) = "on" Then Response.Write "checked"%>><%response.write rs1("ninite")%> </td></tr>
0
 

Author Comment

by:princeservice
ID: 40562756
Still doesn't work. This is really weird. The code at the end <%response.write rs1("ninite")%> was just used to make sure that something was actually being returned. It returns "On" every time.

As of right now the code is:
<tr><td><b>Ninite:</b></td><td><input type="checkbox" name="cbxNinite" id="cbxNinite" <%If CStr(rs1("ninite")) = "on" Then Response.Write "checked"%>></td></tr>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 150 total points
ID: 40562765
Hi,
first of all Big Monty is right that the generated HTML has to be valid - otherwise you might have a lot of side effects.
Can you perhaps post the generated HTML code of your ASP page (not the ASP source)? Can you ensure that the value has no leading / ending space or other whitespace character?
My try:
<tr><td><b>Ninite:</b></td><td><input type="checkbox" name="cbxNinite" id="cbxNinite" <%If Trim(rs1("ninite")) = "on" Then Response.Write "checked" %> /><%response.write "-" & rs1("ninite") & "-"%></td></tr>
<tr><td><b>Ninite:</b></td><td><input type="checkbox" name="cbxNinite" id="cbxNinite" checked />Sample</td></tr>

Open in new window

HTH
Rainer
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40562773
what is the data type coming from the database? boolean or string?
0
 

Author Comment

by:princeservice
ID: 40562786
Yes is does have trailing spaces. The datatype is nchar(10). I do not see an option for boolean. What should I use as a datatype?
0
 

Author Comment

by:princeservice
ID: 40562788
Oh, and the trim did not work.
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40562806
nvarchar is fine, if it was boolean or bit or one of those true/false data types, it would be good to know

try changing your checkbox to

<input type="checkbox" name="cbxNinite" id="cbxNinite" <%If Trim(CStr( rs1("ninite") ) ) = "on" Then Response.Write "checked" %> />
0
 

Author Comment

by:princeservice
ID: 40562823
That did it. I guess combining trim and cstr did the job. Monty - you never seem to fail me. Rainer - couldn't have done it without you.
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40562826
glad that worked because I was running out of ideas :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Automatically log-in a new user through code 1 55
Classic ASP - SQL Server connection problem 5 61
API not working 33 56
Javascript to allow login/password authorization 4 30
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now