Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 127
  • Last Modified:

Populating a checkbox with info from a database - asp classic

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
princeservice
Asked:
princeservice
  • 6
  • 6
2 Solutions
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
 
princeserviceAuthor Commented:
You didn't change anything.
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
princeserviceAuthor Commented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
 
princeserviceAuthor Commented:
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
 
Rainer JeschorCommented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
what is the data type coming from the database? boolean or string?
0
 
princeserviceAuthor Commented:
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
 
princeserviceAuthor Commented:
Oh, and the trim did not work.
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
 
princeserviceAuthor Commented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
glad that worked because I was running out of ideas :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now