Solved

Populating a checkbox with info from a database - asp classic

Posted on 2015-01-21
13
121 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 33

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 33

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 33

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
 
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 33

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 33

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 33

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…

733 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