473,473 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

REPOST - Conditionally checking a CheckBoxList item if record is found..

OK - I'm looking for the best approach on how to do this.

I have a form page where the user can edit their "Profile" (data) which is
in SQL Server. It's your basic company information - address, phone
numbers, etc. I also have on the form a CheckListBox which is bound to
another table in SQL Server - let's assume it the USA States so there's 50
checkboxes dynamically written to the page. When the user "adds" their
data, they check all the states that their company operates in and submits
the form. Easy enough. They might check 1 state - they might check all 50.
It just depends. Then I have a function that saves all data to two tables.

The basic company data is saved in the Company table (duh!). The states
selections are saved in a many to many table which is designed like this:

ID | CompanyID | StateID
=======================
1 | 1 | 1
2 | 1 | 7

etc...

I simply loop thru all the the CheckBox items and if one is check, INSERT a
record and repeat.
For Each itmStates In chkStates.Items
If itmStates .Selected Then
strSQL = "INSERT INTO ........"
cmdInsert = New SqlCommand(strSQL, conSQLServer)
cmdInsert.ExecuteNonQuery()
cmdInsert = Nothing
End If
Next
This all works fine.

Now here's where it gets tricky. I've copied the form and started making
modifications to make it an EDIT form - where the user can EDIT their
existing data. When the form loads, I simply preload all the TextBoxes with
their data. But how can I do this for the section of the CheckBoxList which
is bound to a SqlDataReader? How can I determine if each and every state as
it's being created in the CheckBoxList if it exists for that user in the
Many-to-may table? Make sense?

After thinking about it, I'm assuming I need to create some kind of Function
to run a check to look for each state as it's being bound and "select" the
checkbox if it's found in the DataReader. Correct? Maybe?

I see that there's a OnDataBinding attribute for the CBL. Would I use this
to call a function?

Here's kind of what I'm picturing. Am I close? Thanks in advance for
anyone who reads this.
I actually did this a couple of years ago with classic ASP but it's pretty
much spaghetti code and I'm redoing it in .NET.
<asp:CheckBoxList id="chkStates" runat="server" OnDataBinding="<%#
MyFunction(CompanyID) %>" />
and then my pseudo logic...

MyFunction(intCompanyID As Integer) As Boolean(?) String? ListItem??

'Create DataReader from M-T-M table based upon Company ID
' SELECT * FROM TABLE WHERE COMPANYID = intCompanyID
'DO ...loop thru rows
' If current row is matches the current State checkbox, then set the
checkbox item to "selected" (how?)
' End If
' LOOP WHILE

End Function


Nov 18 '05 #1
0 1590

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bryce Budd | last post by:
Hello All, I've been a taker of information from newsgroups for a long time and thought I'd finally make a contribution back to the community whose supported me when I've needed it. After all...
4
by: dm_dal | last post by:
Is there a know issue surrounding the CheckBoxList control and it's viewstate? When my control is created, it's ListItems are checked as needed, but on a postback, they loose their Selected...
4
by: Shaul Feldman | last post by:
Hello, I have something really awkward at work - fighting with CheckBoxList... How can I define CSS for ListItem in CheckBoxList programmatically. I add CheckBoxList's Items on the fly, something...
0
by: D. Shane Fowlkes | last post by:
OK - I'm looking for the best approach on how to do this. I have a form page where the user can edit their "Profile" (data) which is in SQL Server. It's your basic company information - address,...
5
by: Patrick.O.Ige | last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist) By doing "li.Selected = True" i can see all the checkBoxes are selected. But what i want is to be able...
4
by: Patrick.O.Ige | last post by:
I have a CheckBoxList in a DataList and i'm trying to get item Selected after doing a postBack. I have set my CheckBoxlist AutoPostBack="True" Any ideas what 'm doing wrong? It seems not to...
2
by: jase_rw | last post by:
Hi There, Having trouble with a CheckBoxList in VB.NET. I've a CheckBoxList on a webform. I read some data from a database and try and check the appropriate boxes based on the data....
0
by: webmaster | last post by:
Hi all, I'm tearing my hair out with this one. I have successfully implemented by own RadioButtonList in order to provide additional functionality and a DIV rather than TABLE-based layout in...
1
by: terminul | last post by:
Hi I have a datalist which loops through the categories and within the datalist I have a CheckBoxList which are bound on the DataList's OnItemDataBound. The only problem I have is retrieving...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.