473,387 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Best Approach? 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 1523

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

Similar topics

16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
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...
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...
3
by: onlyprad | last post by:
Hi, I am populating the checkboxlist in (!IsPostBack) block of page load event with some checked items. After populating with checked items I am iterating through the items collection and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.