retrieving checkboxlist values from database - only selects one value!

Hi

I'm using a CheckBoxList server control and trying to select certain values which are retrieved from a database.  How ever, i'm only getting one checkbox 'checked' even when there are multiple values.

 

Public Sub getFacilities(ByVal queryString As String, ByVal connectionString As SqlConnection)
        Using connectionString
            connectionString.Open()

            Dim command As New SqlCommand(queryString, connectionString)
            Dim reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
                CType(FormView1.FindControl("facilityCheckBoxList"), CheckBoxList).SelectedValue = reader(0)
            End While
        End Using
    End Sub

 

 The single value which is selected is correct, and i have also run the SQL command seperatly which shows that there should be multiple values!

 

thanks
 

0
mattock
2/19/2007 12:57:34 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

2 Replies
3346 Views

Similar Articles

[PageSpeed] 34

Hi!,

This is happening because you r resetting selectedvalue everytime while running the loop, so in the end of the loop it gives u the selected value for the last value.

U have to check each item for the checkboxlist and find a match for the value from the sqldatareader, if matches then set that Checkboxlist item's selected property = true;

While reader.Read()

{

for (int i = 0; i < chk1.Items.Count;i++)

{

if (reader(0) == chk1.Items[i].Value)

chk1.Items[i].Selected =

true;

}

}

Hope this will help.


Regards
1
kakali
2/19/2007 1:40:11 PM

Hi

Thats spot on, thanks for that - i've converted somthing similiar from above to VB if anyones interested:

 
 

Dim command As New SqlCommand(queryStringFac, strConn)
            Dim reader As SqlDataReader = command.ExecuteReader()
            Dim chk1 As CheckBoxList = CType(FormView1.FindControl("facilityCheckBoxList"), CheckBoxList)
            Dim num As Integer = (chk1.Items.Count) - 1

            While reader.Read()
                Dim i As Integer
                For i = 0 To num
                    If reader(0) = chk1.Items(i).Value Then
                        chk1.Items(i).Selected = True
                    End If
                Next

            End While
 
 
0
mattock
2/19/2007 2:37:33 PM
Reply:

Similar Artilces:

check if two values are unique... if one value is form the database
Hi My question has to do with using an if statement, or an "is unique" kinda statement to figure out if two values are distinct from each other. One value will be a session and the other will be pulled from an sql database. I am using vb and asp.net.If anyone can tell me how to use the if statement with a database value... such as #Eval that would be awesome.A sample of the code is:<% if Session("this_value") = Eval("Name") then response.write("they are the same") end if %> I appreciate any correspondence You have to retieve the value...

Selected value in select list with javascript based off value in database
 I've got a classic asp page with a select list. I need the select list's selected value to be whatever the user selected when they created their account. I've been trying to use <body onLoad="onLoad();"> to call my function, and then I'm trying to specify which value should be selected in the function. Also, when the select list selected values change, there is a javascript function I call to disable text boxes. I think there is an issue with my javascript function, and I can't find any examples of this from good ole' google.  My j...

Selecting value in one field then causes value in another field to be selected.
I have a web form which contains a date field, the date is populated using a DatePicker which is Javascript based. The form also has a drop down box containing 3 values. What I want to do is when a date has been selected that this will cause the dropdown list value to change to dated which is one of its 3 values. i.e. when the date field is not blank then the value will be selected in the dropdown. What I also want to happen is when the date has been selected and the dropdown value is dated then the date field and the dropdown field will be set to read only.Anyone any ideas how to this usin...

Selecting value on Picklist
Hi I have a case, where it is possible to have more than one manager... I found solution how I can handle multiple managers by adding picklist field on request form -> allow only single selection by user / require user to select manager from field - Selection is saved to flowdata.manager and approval addressee is picked from it For better usabilty of form it would be great if picklist with only one manager value selected automatically (most cases) - what could be the code for achieving that? How to test if field has more than one value? -- vm_luotonen ---------...

How Select one dropdownlist value using down arrows corresponding other dropdownlist value are select
I am using two dropdownlist box.One is used for country selection. other one is used for state selection.I am using dropdown selectedindexchanged event. when i click country dropdownlist corresponding state is selected in state dropdownlist box. How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.R.Karthikeyan   kartmca: How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.Generally, we don't implmenet...

About passing a form value to a Database to retrieve if user exists in Database
Hi all,   I was working on program that gets a username and passwod and retrives if  a user exists in the Database. would it be legitiamate to pass the textbox value to the database as follows:  1 string Username = txtUserName.Text; 2 3 string Password = txtPassword.Text; 4 5 boolean Success = false; 6 7 // use the above declared variables to match the use existance in the datbase. 8 9 string query = "SELECT Roles from UserLogin WHERE UserName = @ Username" + "AND password = @ Password"; 10 11 Sucess = true; 12 13 ...

Trying to enter more than one selected value into one column in the database.
Hi i am trying to enter more than one value into my constructor field in my database, at the moment when the values are entered into the database through the front end, they appear in the database as seperate records however they have the same ID,  the only thing that changes is are the data in the fields which i want in just one. This is the part of the stored procedure I am sure is causing this to happen; IF @Access_Right_ID=8  or @Access_Right_ID=3  or @Access_Right_ID=9   BEGIN     DECLARE @Delimiter char(1)     ...

Dropdownlist selected value passes first value in list no matter what value is selected. why ?
 HI . I have a dropdownlist. Databinds ok but when I must pass a value as a Selected value it always passes the first value in the list even if I select another value. I tried to set the ddl to enableviewstate false but it doesn't change anything. Any ideas ? Code below, thanksLoading the ddl:       productCategories = CatalogAdmin.Admin_Product_Categories_NotIn(productId);        for (int i = 0; i < productCategories.Rows.Count; i++)        {       &...

Populating TextBox Value with Values Retrieved From Database
Hi. I recently encounted a problem and was hoping you all here can help me. You see. I wanted to create an administrative section where the administrator can edit an existing News entry. I didn't want to use DataGrid because the content of each News entry is going to be extremely long and would screw up the whole DataGrid. What I did was create a form with TextBoxes for the Date Posted and Title and Content of the News entry. Then, on Page_Load, the ASP.Net page will do a SELECT query to retrieve the data for the selected New entry and fill the TextBoxes accordingly. The problem is,...

DropDownList value selected base on database value?
Hi, is there anyway how I can allow my dropdownlist (on pageload event) to select the value base on the value found in the database, in addition I also want the dropdownlist to contain other values too however the selectedValue must be the one found in the database. Sorry if it sounds confusing. I'm using VB.NET 2003 with ASP.NET Thanks. Hi,Whatever you want to get selected for your dropdown use following code.if(dropdownid.items.findbyvalue("value") != null){    dropdownid.items.selecteditem = false;    dropdownid.items.findbyvalue("value").selected = t...

retrieve selected value from database
Hi guys, From my question, for sure u know that i'm new to asp.net even though i hav 2 years experience in asp. How can we retrieve a selected value form database. Assume that a table have 5 columns named name, age, address, sex and postcode And i juz wanna retrieve name and address from the database. Do help me guys Thnx for any advice and i really appreciate it. You need to use the following statement SELECT name, address from table Then you need to lay it out with a DataGrid, which is binding the values to the browser will display them. There's a decent tutorial on th...

Passing Values from a Web Form to Another Web Form
Hello All! I am trying to pass values from one web form to another web form. I know that there are 3 ways of doing this: queryString, Server.Transfer and Session. I am using the Server.Transfer method. But I am not able to create, in my 2nd web form, an instance of my 1st web form. My 1st Web form is named CallTrack.aspx and my 2nd is named Export.aspx. What I mean is something like this: Public value as CallTrack. I can't create it in my 2nd web form because when the intellisense is displayed, there's no CallTrack in the options. Thanks!   How about using cross-...

Drop down list in Form view returns error when value in bound field (data binding
I get the following error when I click edit to edit the details in the web form:'ddlUserRestrictionType' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value  I would like it to accept any value which currently exists in the database. Is this easily achievable? With what you've posted, all I can tell is that you are trying to set the SelectedValue of a ddl to a value that does not exist in the list.  It would be helpful if you can post some code and the expected values of this control.~ Timwww.bucketo...

can i drag the selected value of dropdown list in one web part to another web part
hi, supose i am having time displaying in on webpart can i drag the value of time to another web part and and display the time is: and the value draged from the other webpart so i want to drag the value year and population into antoher web part and according to that the grap must appear i.e what  i exactly want is i must drag some value from one webpart to onother webpart and according to event some thing must happen in another web part   Hi, The problem has been replied in the following post: http://forums.asp.net/t/1141884.aspx But from the title you described, do ...

How do .NET Developers handle the need for a RadioButtonList to default to "no selected values" until user selects one?
If I am trying to capture user information in a database through a radiobuttonlist without giving any preselected items, it seems the only possible way to do this is to set the default value of the database to -1 and bind it to the selectedindex value.  However, when the user starts making selections, the selectedindex value is NOT the same as the selectedvalue that the user is clicking.  So the database is collecting selectedindex value.  Is there a way to handle this scenerio correctly?  I originally had the selectedvalue bound to the database, but could not get an empty radiobuttonlist.  Now I have the selectedindex bound to the database with a default value of -1, but the user selection is not the same as the index.  It seems like this would be a very common problem for anyone using a radiolistbutton. Thanks for any help with this. In most cases I never use the databound capability of radio button lists or drop down lists just because of situations like this. Just build it manually and then you can ensure that no value is preselected.C# / C++ Developer...

Select values from one table based upon values in another...
How do I:Select f1, f2, f3, from tb1 where f1=Select f1 from tb2 where f1='condition'?Rex WinnCode-frog System Architects, Inc.VS 2005 Professional, C#, C++ Well... You might try reading up on some standard sql syntax... There are a number of different ways... Select t.f1,t.f2,t.f3 From tb1 t INNER JOIN tb2 t2 ON t.f1 = t2.f1 Where t2.f1 = 'condition'   Select t.f1,t.f2,t.f3 From tb1 t Where t.f1 IN (    Select t2.f1    From tb2 t2    where t2.f1 = 'condition')   Select t.f1,t.f2,t.f3 From tb1 t Where exists (select t2....

CheckBoxList
I am wondering how I should save and retrieve values for a checkboxlist link to a database?Ideally I would like to use one field per checkboxlist in my db. Thanks If I read this correctly you want to use on Datacolumn in your DB to hold all check values of you checkboxlist. If this is correct then this is how you do that. What I have done is create a string and iterate through the list appending any checked values ot the string. Examples you have 5 checkboxes in your list so you set the value of each check box to a, b, c, d, e and you check 1, 3, 5 your string would be ace. Then wh...

adding a dropdownlist to a gridview and fill and then select the value on the database value
I am lost I want to add a dropdown list to a gridview (2005) fill the ddlist and then fill the gridview from a query and have the query value as the selected value by default.  hi !!Put your Dropdownlist in your <itemTemplate> and Fill it in GridView's RowDataBound Event..For that you need to find the DropDownList from that perticular Row by writing FindConrol Method of DataRow....LikeDropDownList DropDownList1 = (DropDownList)e.Row.FindControl("DropDownList1");ThanxHardik PatelThanx,Hardik Patel Hardik, you are right about putting the ddl in a templatefield.Howe...

If Not value or value then....
Hi all,I want to write a simple IF Not then statement in ASP.net 2.0 and I would like some help please.  If Not ntuser = "DOMAIN\ntuser" Or ntuser = "DOMAIN\ntuser2" Then Panel1.Visible = False End If Doesnt work, can you tell me where i'm going wrong please?many thanks.  The first place you're going wrong is in telling us it "doesn't work" without providing any details. JeffPlease: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue. Use Not as stated below. It ...

How to set a dropdown value in a form based on a value read from a database?
We wish to populate a form based on information read from our database.One of the controls is a dropdown list. We first fill up the ddl from a table of about 60 values. Then we read a value from a different table that was saved data earlier to see which of the 60 values was picked. We wish to show the user that value in the dropdownlist when we display the form. How can we do this?For example, we fill the dropdown list with school course codes (French, German, Math, etc.) , then wish to read a different table in the database and select the dropdown item based on what we r...

How to save and retrieve checkboxlist values in/from database?
Hi experts,    I want to save the value of the checkboxlist to database and then retrieve it on another application.I have a working code to put all selected listitems to a string, but HOW TO eliminate the last ","  ??For example, one value saved in database is like "Weekdays, Weekend, Shift," and I need it to be saved like "Weekdays, Weekend, Shift" Also, HOW TO RETRIEVE IT into checkboxlist listitems on web form from the database? Thank you in advance..................................................... <asp:checkboxlist id="CkblstSchedule" RunAt="server" SelectionMo...

getting value 1 from a selected checkbox & saving the value into a database
Hi, I m new to ASP... I m building a page with few checkboxes & text boxes.. its a simple checklist you can say.. However, what i am looking for is when a check box is checked & the submit button is clicked, the value of that checkbox as 1 should be saved in the database. How to acheive this... any help would be really appreciated... Thanks Aadil  in the buttons click event look and see if the check boxes are checked and save in the db if so  if(chk1.Checked) {         //save in db      }Mike Starkey MCP, MCTS Hi Aadil you c...

select the row of the datagrid whose value is same as one of the value of row of datagrid
hellow all,  I have a datagrid which is bound to the ordertable,so all the orderdetails are shown on the page load. but my requirement is that,i have a textbox and a search button,when the user enter the order number in the text box and click on the search button the datagrid should show that record whose order number is entered by the user n most imp that the record may be in the 65th or 34th row of the datagrid,i wants to put it in the first row. how to do it. If there's a reason you can't filter down to just the one row, you'll have to come up with some paging algorithm that wil...

to change value of Client Side Select html control from database value....
hello forum,  I have a select .. Demo.aspx <select id="DropAccType" name="DropAccType"> <option>Select</option> <option>Saving</option> <option >Current</option> <option>Regular</option> <option>Demat</option> </select> I want to set the value of select by fetching the data from daabase i.e. In my table if I have Value current then when i request the demo.aspx then in select Control the value Current should get selected..... Please help me to sort this out...   Thnx in advance.. Hi-I could be...

Web resources about - retrieving checkboxlist values from database - only selects one value! - asp.net.web-forms

APNS Pusher, debug your push notification app - Coders Grid
Coders Grid your best development resources collections site Menu Skip to content Resource web development design stuffs guideline coding mobile ...

.NET sample code - developer Fusion
Discover the very latest .NET sample code Out of the box, the ASP.NET list controls (CheckBoxList, RadioButtonList, DropDownList) only support ...

ConditionalRequiredTextValidator Control
The ConditionalRequiredTextValidator control allows you to specify a CheckBox, RadioButton, CheckBoxList, RadioButtonList, DropDownList, or ListBox ...

Sports-Gazer - About Us
Sports-Gazer is an application for ranking and rating sports teams. It also can project scores for upcoming games. Certain parts of this application ...

Layar 3.0 mobile augmented reality browser launched
Layar, the world’s first augmented reality (AR) browser…

ASP.NET 4 and Visual Studio 2010 Web Development Overview : The Official Microsoft ASP.NET Site
This document provides an overview of many of the new features for ASP.NET that are included in the.NET Framework 4 and in Visual Studio 2010. ...

Gaiaware - Gaia Ajax
Home Samples Download Free Trial Buy from $195 Login Download Free Trial ASP.NET Done Right 6 reasons to use Gaia Ajax? 40 UI controls 10 aspects/behaviors ...

compass - Coders Grid
Coders Grid your best development resources collections site Menu Skip to content Resource web development design stuffs guideline coding mobile ...

APNS Pusher, debug your push notification app
Coders Grid your best development resources collections site Menu Skip to content Resource web development design stuffs guideline coding mobile ...

Buttons.css, a css library makes buttons beautiful - Coders Grid
Coders Grid your best development resources collections site Menu Skip to content Resource web development design stuffs guideline coding mobile ...

Resources last updated: 2/16/2016 11:59:46 AM