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
Solved

Populating a checkbox with info from a database - asp classic

Posted on 2015-01-21
13
120 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 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

838 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