View Full Version : asp.net compare two datasets
01-26-2003, 09:30 PM
OK... here's the problem...
I have a sign-up section on my site where people can select interests and hobbies using checkboxes, which needs to be updateable (is that a word??? LOL)
If the user wants to update their entry then I would like to show the full list of hobbies/interests with those they have selected in the past already checked.
I 'sort of' developed a solution that does it, but that requires looping within a loop - very inefficient and is quite a hit on the server performance.
Anyone any ideas as to how this might be done more efficiently???
Prefered language is VB but C# will do just as well.
01-27-2003, 04:11 AM
Are you using the CheckboxList control
Are the Checkbox control's list items static or populated via databinding?
If you are using a CheckBoxList webcontrol here is one way populating the object and setting the check boxes check state.
<%@ Import NameSpace="System" %>
<%@ Import NameSpace="System.Data" %>
<%@ Import NameSpace="System.Data.OleDb" %>
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="windows-1252" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim oDsHobbies As New DataSet()
Dim oDrHobbies As DataRow
Dim sConn As String = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=E:\Inetpub\wwwroot\Code_Test\CheckBoxList\Hobbies.mdb"
' CkeckBoxList DataSet
Dim sSQL = "SELECT ID, Sub_Catagory FROM Hobbies WHERE Sub_Catagory Is Not Null"
Dim oDaHobbbies As OleDbDataAdapter = New OleDbDataAdapter(sSQL, sConn)
chkHobbies.DataSOurce = oDsHobbies.Tables("Hobbies").DefaultView
chkHobbies.DataValueField = "ID"
chkHobbies.DataTextField = "Sub_Catagory"
' Selected Intereset DataSet
sSQL = "SELECT Interests.HobbyID FROM Interests WHERE Interests.UserID=1;"
Dim oDaSelected As OleDbDataAdapter = New OleDbDataAdapter(sSQL, sConn)
' Create DataView for Filtering
'Note: The DataView must have a Sort order set before applying the filter
Dim oDvFind As DataView = New DataView(oDsHobbies.Tables("Interests"))
oDvFind.Sort = "HobbyID"
' Begin adding List Items and
Dim i As Integer = 0
Dim nFound As Integer
For Each oDrHobbies In oDsHobbies.Tables("Hobbies").Rows
nFound = oDvFind.Find(oDrHobbies("ID"))
If nFound <> -1 Then
chkHobbies.Items(i).selected = True
i = i + 1
The comtrol looks like this:
<form id="form1" method="post" runat="server">
<asp:checkboxlist ID="chkHobbies" runat="server" RepeatLayout="Table" TextAlign="Right" RepeatColumns="3" RepeatDirection="Horizontal" />
Hoep This helps :cool:
01-27-2003, 04:13 AM
I would do a SELECT of the answers they already stored and check some checkboxes based on that. Here's a very rough code example.
Suppose you have id, name, answer1, answer2, answer3. They enter their name and they check the boxes for answer1 and answer3. So those boxes will show up as checked.
'open your connection named MyConn
Set RS=MyConn.execute("SELECT answer1, answer2, answer3 from MyTable where ID="&request.form("ID"))
<input type="checkbox" name="answer1" value="yes" <%if RS("answer1")="yes" then response.write "checked"%>
<input type="checkbox" name="answer2" value="yes" <%if RS("answer2")="yes" then response.write "checked"%>
<input type="checkbox" name="answer3" value="yes" <%if RS("answer3")="yes" then response.write "checked"%>
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.