Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 1st, 2008, 07:22 AM
Registered User
 
Join Date: Apr 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default repopulating CheckBoxList from db table problem

Hi to all,

I have a an checkboxlist control in a webform wich populated from a db table values , DataTextField="interest_name" DataValueField="id"



Code:
         

1       <asp:CheckBoxList ID="interests_CheckBoxList" runat="server"   Width="100%" DataSourceID="SqlDataSource1_int" DataTextField="interest_name" DataValueField="id" >
2                        </asp:CheckBoxList><asp:SqlDataSource ID="SqlDataSource1_int" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>"
3                            SelectCommand="SELECT [id], [interest_name] FROM [nterests] WHERE ([active] = @active)">
4                            <SelectParameters>
5                                <asp:Parameter DefaultValue="1" Name="active" Type="Byte" />
6                            </SelectParameters>
7                        </asp:SqlDataSource>

then I have a storedprocedure which takes the selected values and store it to a db table.

Now I want to take these values and repopulate the checkboxlist , I tried to ryn the following function in the page load but something is wrong .. could you please help me how I could do it??




Code:
1    public void pop_checkboxes()
2        {
3    
4    
5           
6            string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
7            SqlConnection sqlConnection1 = new SqlConnection(connectionString);
8    
9                    string CommandText1 = "select int_id from user_interests where user_id=1 ";
10   
11          
12           SqlCommand sqlComm1 = new SqlCommand(CommandText1, sqlConnection1);
13           SqlDataReader reader;
14   
15   
16           try
17           {
18   
19               //open connection
20               sqlConnection1.Open();
21               //execute command
22               reader = sqlComm1.ExecuteReader();
23   
24               CheckBoxList chkbx = (CheckBoxList)form1.FindControl("interests_CheckBoxList");
25               //get results
26               while (reader.Read())
27               {
28                  
29                   ListItem currentCheckBox = chkbx.Items.FindByValue(reader[0].ToString());
30                   if (currentCheckBox != null)
31                   {
32                       currentCheckBox.Selected = true;
33                   }
34               }
35   
36               //close reader
37               reader.Close();
38           }
39           catch
40           {
41              
42               Response.Write("error");
43   
44           }
45           finally
46           {
47               //close connection
48               sqlConnection1.Close();
49           }
50      
51       }







Similar Threads
Thread Thread Starter Forum Replies Last Post
update table from another db table timbal25 SQL Server 2005 3 January 19th, 2008 06:47 AM
Select CheckBoxList Base on Value Retrieve Drom DB derekl ASP.NET 1.0 and 1.1 Basics 6 May 30th, 2006 09:58 AM
Checkboxlist problem sansircar ASP.NET 1.0 and 1.1 Professional 1 March 12th, 2006 01:36 AM
updates problem of db table abhit_kumar MySQL 0 December 17th, 2004 07:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.